Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: same syntax highlighting for different file extensions

Tags:

vim

Is there a way to make same syntax highlighting for different file extensions?

e.g: Same highlighting for

foo.c and foo.my_c_extension
like image 780
kmad Avatar asked Apr 24 '13 18:04

kmad


People also ask

How do I permanently set syntax in Vim?

Add the text, “syntax on” anywhere in the file to enable syntax highlighting permanently for vim editor. Save and close the file by typing ':x'. For disabling the feature, just re-open . vimrc file, change the text “syntax on” to “syntax off” and save the file.

How does syntax highlighting work in Vim?

Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations.

How do I enable color coding in vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.


1 Answers

Vim will set the syntax highlighting based on a buffer's filetype. You may set the filetype via autocmd to match multiple file extensions.

For example, when a file is loaded or created in a buffer having the .c or .my_c_extension extensions, the filetype will be set to c:

" In .vimrc, for example:
autocmd BufRead,BufNewFile *.c,*.my_c_extension set filetype=c

See :help filetype and :help autocmd for more information.

According to the filetype help, you may create ~/.vim/ftdetect/file_extension.vim which contains the autocmd. That will be loaded after other rules, allowing you to override settings previously made by Vim or plugins. This may be preferable to setting it in your .vimrc.

" File: ~/.vim/ftdetect/my_c_extension.vim
autocmd BufRead,BufNewFile *.my_c_extension set filetype=c
like image 97
Michael Berkowski Avatar answered Oct 15 '22 02:10

Michael Berkowski