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?
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 <Bram@vim.org>
" 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
.
-
Why the
if
statement? If I leave it in, the autocmds are not executed for me. – oarfish Nov 10 '15 at 14:28 -
-
Ok, this is weird. I have to
au!
statements in the group, namelyau! BufRead,BufNewFile *.py setfiletype python
and in the line belowau! BufRead,BufNewFile *.tpp setfiletype cpp
. But only the second one actually works. Editing.py
files does not set the file type topython
. I'm not exactly a vim-guru so I'm probably missing something. – oarfish Nov 10 '15 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 Nov 10 '15 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 Nov 10 '15 at 15:20
You could do (in your global .vimrc
):
autocmd BufEnter *.tpp :setlocal filetype=cpp
-
autocmd BufEnter *.cuf :setlocal filetype=fortran
to treat CUDA Fortran files as Fortran – khaverim Jan 27 '18 at 18:38 -
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 Jan 9 '20 at 23:20
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++
-
1This seems to be basically identical to @edi9999's answer. Can you elaborate on why to use
BufRead,BufNewFile
instead ofBufEnter
? – oarfish Sep 5 '18 at 10:02 -
1@oarfish It doesn't matter too much I think, but I prefer
BufRead,BufNewFile
ahead ofBufEnter
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 beforesetlocal
so I also omitted that in my answer. (I might have interpreted this completely wrong, happy to hear further discussion) – tallamjr Sep 5 '18 at 10:50