40

I edit C++ code in .cpp files, while template code goes into .tcc files. When I open such a file, no syntax highlighting is available. I tried set syntax=cpp in a a tpp.vim file in ~/.vim/ftplugin/, but it does not do anything, while in the editor it works. Is there some way to have vim treat specific extensions as synonymous with others?

1
  • 1
    Very useful question! Thanks!!
    – rmbianchi
    Commented Feb 24, 2021 at 14:40

3 Answers 3

33

The preferred approach is to create a ~/.vim/filetype.vim, as mentioned in :help 43.2 and explained in Vim FAQ 26.8:

A better alternative to the above approach is to create a filetype.vim file in the ~/.vim directory (or in one of the directories specified in the 'runtimepath' option) and add the following lines:

" my filetype file
if exists("did_load_filetypes")
    finish
endif
augroup filetypedetect
    au! BufRead,BufNewFile *.x       setfiletype c
augroup END

Edit:

The did_load_filetypes shouldn't cause you any problems; it would only require a Vim restart after it is changed.

It is necessary in order to avoid multiple and unnecessary loading of this file, and it is inherited from the base file ($VIMRUNTIME/filetype.vim):

" Vim support file to detect file types
"
" Maintainer:   Bram Moolenaar <[email protected]>
" Last Change:  2014 Jun 12

" Listen very carefully, I will say this only once
if exists("did_load_filetypes")
  finish
endif
let did_load_filetypes = 1

For more information check :help new-filetype.

7
  • Why the if statement? If I leave it in, the autocmds are not executed for me.
    – oarfish
    Commented Nov 10, 2015 at 14:28
  • @oarfish even if you restart Vim?
    – mMontu
    Commented Nov 10, 2015 at 15:05
  • Ok, this is weird. I have to au! statements in the group, namely au! BufRead,BufNewFile *.py setfiletype python and in the line below au! BufRead,BufNewFile *.tpp setfiletype cpp. But only the second one actually works. Editing .py files does not set the file type to python. I'm not exactly a vim-guru so I'm probably missing something.
    – oarfish
    Commented Nov 10, 2015 at 15:11
  • @oarfish if you check the file I mentioned on the edit you will find that it already contains a line with au BufNewFile,BufRead *.py,*.pyw setf python, so your first line is irrelevant. Your problem is probably elsewhere.
    – mMontu
    Commented Nov 10, 2015 at 15:16
  • "Editing .py files does not set the file type to python" - what is the output of :set ft on that case? If it is not python, try following the procedure in Vim-FAQ 2.5 - I have a "xyz" (some) problem with Vim
    – mMontu
    Commented Nov 10, 2015 at 15:20
26

You could do (in your global .vimrc):

autocmd BufEnter *.tpp :setlocal filetype=cpp
3
  • autocmd BufEnter *.cuf :setlocal filetype=fortran to treat CUDA Fortran files as Fortran
    – khaverim
    Commented Jan 27, 2018 at 18:38
  • 2
    If you have multiple files you want to associate with a language, delimit the patterns with commas and no spaces between the commas: autocmd BufEnter *.tsx,*.jsx,*.ts :setlocal filetype=javascript
    – ggorlen
    Commented Jan 9, 2020 at 23:20
  • Upvoted this answer because it works directly from the .vimrc file. Commented Mar 23 at 13:44
1

One could also have: au BufRead,BufNewFile *.tpp setlocal filetype=cpp in their .vimrc such that upon opening a file with .tpp extension, filetype is set for C++

2
  • 1
    This seems to be basically identical to @edi9999's answer. Can you elaborate on why to use BufRead,BufNewFile instead of BufEnter?
    – oarfish
    Commented Sep 5, 2018 at 10:02
  • 1
    @oarfish It doesn't matter too much I think, but I prefer BufRead,BufNewFile ahead of BufEnter because in the docs (:help BufEnter) it states: "BufEnter: .... Also executed when starting to edit a buffer, after the BufReadPost autocommands." where BufReadPost is synonymous with BufRead. In addition, I do not believe the : is required before setlocal so I also omitted that in my answer. (I might have interpreted this completely wrong, happy to hear further discussion)
    – tallamjr
    Commented Sep 5, 2018 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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