Vim launches autocmd for all file types EXCEPT

I have an autocmd Vim that removes trailing spaces in files before writing. I want this in almost 100% of cases, but there are several types of files that I would like to disable. The usual wisdom is to list the types of files that you want autocmd to execute in a comma-separated list, for example:

autocmd BufWritePre *.rb, *.js, *.pl

But in this case it would be burdensome.

Is there a way to map the autocmd template to all EXCEPT files of those matching the template? I cannot find the equivalent of non-matching in docs.

+56
vim autocmd
Jun 27 '11 at 17:45
source share
3 answers

*.rb not a file type. This is a file template. ruby is a file type and can even be installed on files that do not have the .rb extension. So what you most likely want is a function that your autorun calls to check for file types that should not be affected and separates spaces.

 fun! StripTrailingWhitespace() " Don't strip on these filetypes if &ft =~ 'ruby\|javascript\|perl' return endif %s/\s\+$//e endfun autocmd BufWritePre * call StripTrailingWhitespace() 



Based on evan's answer, you can check the local buffer variable and determine whether to use this strip. It will also allow you to disable one-time operation if you decide that you do not want to delete the buffer, which usually gives the file type.

 fun! StripTrailingWhitespace() " Only strip if the b:noStripeWhitespace variable isn't set if exists('b:noStripWhitespace') return endif %s/\s\+$//e endfun autocmd BufWritePre * call StripTrailingWhitespace() autocmd FileType ruby,javascript,perl let b:noStripWhitespace=1 
+44
Jun 27 2018-11-18T00:
source share

Another choice of one method:

 let blacklist = ['rb', 'js', 'pl'] autocmd BufWritePre * if index(blacklist, &ft) < 0 | do somthing you like 

Then you can do something that you like for all types of files except those that are blacklisted.

Maybe you will be useful :)

+38
May 2 '12 at 8:58
source share

The easiest way is to set the local variable for one file type to true. Then set the auto-computer if this variable is false (if set for everything else) or if it exists at all (no need to set it).

 autocmd BufWritePre *.foo let b:foo=true if !exists("b:foo") autocmd ... endif 

modified variable prefixes based on comments

+10
Jun 27 '11 at 18:13
source share



All Articles