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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With