0

I want the filetype .my_cpp and .my_cc to behave exactly like C++ files in Vim. I have the following set:

autocmd BufNewFile,BufRead *.my_cc set syntax=cpp
autocmd BufNewFile,BufRead *.my_cpp set syntax=cpp

Although the syntax highlighting works, the indentation doesn't seem to be working like C++ at all. How can I fix this? I assume it would have something to do with indentexpr but I don't know enough about Vim to make it work. Doing set indentexpr? in an actual .cc file gives me indentexpr=, so I guess it's not set to anything?

I would appreciate any help on the matter.

2

Add

autocmd BufNewFile,BufRead *.my_cpp setlocal filetype=cpp

Read :help :setfiletype and decide if it applies to your or not.


Even better would be to create ~/.vim/ftdetect/my_cpp.vim with

autcmd BufRead,BufNewFile *.my_cpp setlocal filetype=cpp

You won't need to set syntax.

If you do instead :setlocal filetype=my_cpp than you can create ~/.vim/after/ftplugin/my_cpp.vim and ~/.vim/after/syntax/my_cpp.vim.

You can make them behave like cpp filetypes with

  • :runtime! ftplugin/cpp.vim ftplugin/cpp_*.vim ftplugin/cpp/*.vim
  • :runtime! syntax/cpp.vim

respectively.

As @filbranden points out:

For indentation, you might need an indent/my_cpp.vim as well. If you define your own syntax file, you might want to set b:current_syntax appropriately (even if all you do is source the original cpp syntax files.)

Note that if you just setlocal filetype=cpp everything is taken care of for you. The reason not to would be if you needed the cpp filetype and some (usually small) extensions.

6
  • 1
    For indentation, you might need an indent/my_cpp.vim as well. If you define your own syntax file, you might want to set b:current_syntax appropriately (even if all you do is source the original cpp syntax files.) – filbranden Nov 21 '20 at 19:20
  • 1
    Thank you Ben and @filbranden. I'm not writing an extension or anything, just want these files to be detected as cpp and behave exactly like them, mainly talking about indentation and syntax highlighting here. Hopefully autcmd BufRead,BufNewFile *.my_cpp setlocal filetype=cpp in ~/.vim/ftdetect/my_cpp.vim will be enough. Is the ~/.vim/indent/my_cpp.vim necessary if I am not writing extensions/plugins? – herophant Nov 21 '20 at 19:53
  • 1
    If you're setting the filetype to cpp (which is definitely the easiest) then the ftdetect file with the one line is all you need! – filbranden Nov 21 '20 at 20:13
  • 1
    You can put many autocmd lines in one file @herophant – D. Ben Knoble Nov 22 '20 at 0:25
  • 1
    @herophant The file name doesn't really matter (well, as long as it's in the right directory and it has a .vim extension)... Usually you want it to be called something unique (so it doesn't conflict with other existing ones.) You can add several autocmd's in the same ftdetect file, yes. – filbranden Nov 22 '20 at 0:26

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.