Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make VIM behave differently for different filetypes?

Tags:

vim

I have some character maps for typing braces that I'd like to behave differently based on the file's extension - I imagine that would be pretty useful ability to have in general.

Any ideas on how to make this happen from my .vimrc or a plugin?

Thank you!

like image 930
Nicky Hajal Avatar asked Dec 07 '22 01:12

Nicky Hajal


2 Answers

There are basically two ways. Use the filetype plugin, or use filetype or extension autocommands.

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 better solution, particularly if you have alot of custom commands for a filetype, is to use the files ~/.vim/after/ftplugin/<filetype>.vim for each filetype. Commands in those files will be executed after loading a file of the given type.

like image 119
jkerian Avatar answered Feb 08 '23 23:02

jkerian


In complement to jkerian's answer, here are a few links to other questions related to ftplugins (as script-local variables, mappings, command, settings, abbreviations, etc shall be used when writing ft specific configuration):

  • Multiple autocommands in vim
  • gVim and multiple programming languages
like image 30
Luc Hermitte Avatar answered Feb 08 '23 22:02

Luc Hermitte