简体   繁体   中英

How do I set a .vimrc configuration for many file types?

Let's say I want one group of settings for HTML, CSS and JavaScript files but another set for Ruby files (completely different). What's the simplest way to do this?

You can give global settings like this.

For other files:

set shiftwidth=4

for Ruby files:

autocmd BufRead,BufNewFile *.rb set shiftwidth=2

You can get what you want via autocmd , it's format as follows:

au[tocmd] [group] {event} {pattern} [nested] {cmd}

[group] and [nested] are optional.As I give the example above, BufRead,BufNewFile is the event, *.rb is the pattern, and set shiftwidth=2 is the cmd.

more information about autocmd , please refer: :help automcd

You can put ftplugin directory with filetype-specific settings inside .vim directory

.vim
└── ftplugin
    └── ruby.vim
    └── markdown.vim

And keep your settings there. The are applied when file with corresponding filetype is opened.

Also, you might need to have filetype detection(if not detected properly). You can put this to your .vimrc

autocmd BufNewFile,BufRead *.markdown,*.md,*.mdown,*.mkd,*.mkdn set ft=markdown

Or, put it into ftdetect directory

.vim
└── ftdetect
    └── markdown.vim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM