Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable autocmd project specific in vim

Tags:

vim

macvim

I have given configuration to remove trailing white spaces in vim

  au BufWritePre *.rb :%s/\s\+$//e
  au BufWritePre *.html :%s/\s\+$//e
  au BufWritePre *.js :%s/\s\+$//e
  au BufWritePre *.jsx :%s/\s\+$//e
  au BufWritePre *.json :%s/\s\+$//e

PROBLEM

I have a project in which multiple developers are working and it's a big one. I don't want to cause lots of conflicts by removing existing trailing whitespace in this project.

EXPECTED

I want to disable this configuration for that specific project only. Lets say my project is in ~/work/projectA.

like image 222
przbadu Avatar asked Oct 21 '25 15:10

przbadu


1 Answers

First, you could do all that with a single autocommand:

au BufWritePre *.{rb,html,js,jsx,json} %s/\s\+$//e

Second, you should put it inside a proper "augroup":

augroup TrailingSpaces
    autocmd!
    autocmd BufWritePre *.{rb,html,js,jsx,json} %s/\s\+$//e
augroup END

Third, you could disable that group for that specific project with another (maybe a bit heavy-handed) autocommand:

augroup SpecialProject
    autocmd!
    autocmd BufNewFile,BufRead ~/work/projectA/**/* autocmd! TrailingSpaces
augroup END

Fourth, losing your cursor position after :%s/\s\+$//e is not fun. I would do something like that if I were you:

augroup TrailingSpaces
    autocmd!
    autocmd BufWritePre *.{rb,html,js,jsx,json} let w:wv = winsaveview() | %s/\s\+$//e | call winrestview(w:wv)
augroup END

See :help winsaveview().

like image 169
romainl Avatar answered Oct 23 '25 07:10

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!