Vim run autocmd on all filetypes EXCEPT
Asked Answered
M

7

96

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.

Matthia answered 27/6, 2011 at 17:45 Comment(0)
G
75

*.rb isn't a filetype. It's a file pattern. ruby is the filetype and could even be set on files that don't have a .rb extension. So, what you most likely want is a function that your autocmd calls to both check for filetypes which shouldn't be acted on and strips the whitespace.

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

autocmd BufWritePre * call StripTrailingWhitespace()

Building on evan's answer, you could check for a buffer-local variable and determine whether to do the strip using that. This would also allow you to do one-off disabling if you decided that you don't want to strip a buffer that's a filetype you normally would strip.

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
Glyptodont answered 27/6, 2011 at 18:4 Comment(2)
Right, I was hoping for a pattern that would exclude certain file extensions and definitely misused the terms in my description. While I was hoping for a one-liner, wrapping the logic in a function and calling it isn't too shabby. Thanks!Matthia
If you want to have your vim config more modular, you can add the setting let b:noStripWhitespace into the files in the filetype plugin section. F.x. to disable trailing whitespace removal for git commits, add the line to ~/.vim/ftplugin/gitcommit.vim.Greenhaw
R
67

Another choice of one line way:

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

Then you can do something you like for all filetypes except those in blacklist.

Rodent answered 2/5, 2012 at 8:58 Comment(2)
Want to point that this didn't work for me(not sure if it's because of neovim or my vim version). But my below suggestion did work.Lascar
I'm pretty sure it doesn't work because it also needs a | endif after the "do something you like" bit.Criswell
C
19

A good way would be to set a local variable for the one filetype to true. Then set the automcommand if that variable is false (if set for everything else) or if it exists at all (no need to preset it).

autocmd BufWritePre *.foo let b:foo=true

if !exists("b:foo")
    autocmd ...
endif

changed variable prefixes based on comment

Chart answered 27/6, 2011 at 18:13 Comment(0)
L
10

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.

Lascar answered 9/5, 2021 at 23:38 Comment(0)
P
3

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

au Syntax *\(^rst\)\@<! …
Percipient answered 7/3, 2020 at 12:28 Comment(0)
D
1

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

Derogate answered 3/7, 2020 at 9:42 Comment(0)
G
1

This is very late, but I think you can use two autocmd, one does the 'default' behavior and the other does the 'except' behavior. One example is when I want to set tab size to four for all file types, except HTML, in which I want it to be two. I can configure it like this:

# for all file types
autocmd FileType * se tabstop=4
# except HTML
autocmd FileType html se tabstop=2
Griffith answered 13/2 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.