How do I run an autocommand on every filetype but one?

971 views Asked by At

I have my vim set up to save files whenever I change buffers and on checktime. The problem is that I use Netrw and end up saving Netrw buffers. Can I run an autocommand on every type of file except netrw?

1

There are 1 answers

0
Peter Rincker On BEST ANSWER

You can use an :if in your autocmd to guard against netrw files. e.g.

autocmd FileType * if &ft != 'netrw' | echo "do something" | endif

However this still isn't quite right. You have stopped from saving netrw buffers, but there are other buffers that shouldn't be saved. I would suggest checking 'buftype' and looking for files that start with a protocol e.g. foo://.

Here is an example of auto creating intermediary directories using such an approach:

" create parent directories
" https://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
    if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
        let dir=fnamemodify(a:file, ':h')
        if !isdirectory(dir)
            call mkdir(dir, 'p')
        endif
    endif
endfunction
augroup BWCCreateDir
    autocmd!
    autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END