Question

On save/write in VIM I want to trim trailing whitespace and ensure that the file's line endings are UNIX and not DOS.

Both of these commands work on their own, but I can't figure out how to get them to BOTH run on write/save of a file in VIM.

autocmd BufWritePre,FileWritePre * :%s/\s\+$//e
autocmd BufWritePre,FileWritePre * :%s/\r$//

I've tried all of the following and still VIM throws errors:

autocmd BufWritePre,FileWritePre * :%s/\s\+$//e|:%s/\r$//

OR

autocmd BufWritePre,FileWritePre * %s/\s\+$//e|%s/\r$//

AND

augroup prewrites
    autocmd!
    autocmd BufWritePre,FileWritePre * :%s/\s\+$//e
    autocmd BufWritePre,FileWritePre * :%s/\r$//                                                                                             
augroup END 

None of these produce the desired result without errors being thrown by VI. Any help?

Était-ce utile?

La solution

I finally figured it out. The culprit was not ending the second search with an e after. See end result below:

augroup prewrites
   autocmd!
    autocmd BufWritePre,FileWritePre * :%s/\s\+$//e | %s/\r$//e
augroup END 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top