Vim run autocmd on all filetypes EXCEPT

Viewed 19751

I have a Vim autocmd that removes trailing whitespace in files before write. I want this almost 100% of the time, but there are a few filetypes that I'd like it disabled. Conventional wisdom is to list the filetypes you want an autocmd to run against in a comma-separated list, eg:

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

But in this case that would be onerous.

Is there a way to match an autocmd pattern against all files EXCEPT those matching the pattern? I cannot find the equivalent to a NOT matcher in the docs.

6 Answers

You can do the except on the same regexp:

autocmd BufWritePre *\(.out\|.diffs\)\@<! <your_command>

That will do <your_command> for all files extensions except for .out or .diffs.

This works for Syntax autocommands, where the pattern (<match>) is just the filetype. It excludes any rst files:

au Syntax *\(^rst\)\@<! …

Our .vimrc config file runs only once on startup. So if you put an if test at this time, it won't work, because no python file is then currently being edited.

But you can use .vimrc to set up an automatic behaviour: something that vim will do each time it encounters a special condition. The condition can be in your case: "A new file is being editing, and its file type is 'python'". See :h :au

Related