I am try to change the filetype for *.mm files. I have created a mm.vim file in ~/.vim/ftdetect, with the following code:

au BufNewFile,BufRead *.mm set filetype=cpp

But the when opening a file with the extension .mm the filetype is detected as nroff.

I am guessing that NeoVim (version 0.4.3) is overriding my setting of *.mm filetype. Is there anyway around this? Other filetype overrides are working.

Additional information:

:au BufRead *.mm returns

filetypedetect BufReadPost
*.mm call dist#ft#FTmm()
*.mm set filetype=cpp
*.mm set filetype=cpp

But calling :verbose set ft? actually reveals the issue. There was a view saved for the file and it was this that was overriding the settings. I wa not aware that I was automatically saving a view for files

  • What does :au BufRead *.mm return for you? That should help check out what (Neo)Vim thinks it should do with these files... – filbranden May 9 at 18:18
  • 2
    Neovim config must be in ~/.config/nvim, not ~/.vim – Matt May 9 at 18:33
  • returned from :au BufRead *.mm call dist#ft#FTmm() and set filetype=cpp. It looks like the syntax was last set by /usr/local/Cellar/neovim/0.4.3/share/nivm/runtime/syntax/syntax.vim line 32 – mjb May 9 at 18:55
  • Thank you, yes agreed my setup files should be under .config/nvim. But unfortunatly this does not seem to make a difference – mjb May 9 at 18:58
  • 1
    Glad you found it! You can control what is saved in views with 'viewoptions', for example set viewoptions-=options will prevent Vim from saving local options, including the filetype. I'll add an answer a bit later, but glad that your issue is solved! – filbranden May 9 at 19:31

Explaining the steps you can use to troubleshoot such issues.

First, you can use a command such as :au BufRead *.mm to inspect which autocmds exist for that particular extension. You can use that to double check that your configuration actually took effect. (Which it did in your case.)

Second, you can use command :verbose set ft? to inspect the filetype for the current buffer and see where Vim got that filetype setting from.

In your case, I expect this showed that it was coming from a view created by mkview, so that would explain why your setting wasn't taking effect. Well, actually your setting was taking effect but the filetype was later being overwritten by the view being restored.

If you want to keep using views (for example, for manual folds and cursor position), but you still want your filetype (and potentially other local settings, such as shiftwidth, soft tabs, expand tabs, etc.) to be reconfigured every time you reopen the file (to reflect changes in your configuration), you can set 'viewoptions' to configure what is saved in your views.

for example set viewoptions-=options will

For example, to prevent Vim from saving local options, including the filetype, you can use:

set viewoptions-=options

That way Vim won't save any local settings (filetype included) in the saved views. Of course, this will only take effect the next time you save a view. Views that are already saved will have that information preserved and it will be loaded automatically.

You might also want to take a look at this dist#ft#FTmm() function that Vim runtime uses by default for files with the *.mm extension.

If you look at that code, you'll see it will read the first 20 lines of the file and try to find a line starting with either #include, #import or @import. If any of these is found on the first 20 lines, Vim will set the filetype to objcpp (Objective C++), which I imagine might be what you wanted?

It only falls back to nroff if it can't detect the Objective C++ reliably, which it does looking at those 20 first lines of the file.

So maybe ensuring your file will have includes or imports on the first few lines would be enough to have this working out-of-the-box with the most appropriate filetype without the need for any extra configuration?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.