Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I remove trailing blank lines on save, in Vim?

Tags:

vim

I want to remove trailing blank lines (blank lines could also be just a bunch of tabs and spaces).

In my .vimrc I have:

autocmd BufWritePre * :%s#\($\n\s*\)\+\%$## " trim white spaces at the end of file  

This works, however if a file has already had its trailing blank lines removed, I get the following error message:

Error detected while processing BufWrite Auto commands for "*":

How may I fix this?

like image 345
ditto Avatar asked Oct 23 '25 14:10

ditto


1 Answers

From :help :s_flags:

[e]     When the search pattern fails, do not issue an error message.

So you could try:

:%s#\($\n\s*\)\+\%$##e
like image 176
Jair López Avatar answered Oct 25 '25 19:10

Jair López



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!