How can I make VIM behave differently for different types of files?

I have some character maps for a set of curly braces, which I would like to conduct differently based on the file extension. I believe this would be a pretty useful opportunity to have as a whole.

Any ideas on how to do this from my .vimrc or plugin?

Thanks!

+4
source share
2 answers

There are two ways. Use the filetype plugin or use autocommands like file or extension.

The autocommands (placed in your .vimrc / _vimrc) take the form of either

autocmd Filetype cpp set textwidth=100 

or

 autocmd BufRead *.cpp,*.h,*.c set textwidth=100 

(Obviously, set textwidth=100 can be replaced with any other command)

The best solution, especially if you have many custom commands for the file type, is to use ~/.vim/after/ftplugin/<filetype>.vim for each file type. Commands in these files will be executed after loading a file of this type.

+14
source

In addition to the jkerian answer , here are some links to other questions related to ftplugins (like script local variables, displays, commands, settings, abbreviations, etc., should be used when writing a specific configuration):

+1
source

All Articles