Getting rid of block characters at the end of a line in vim
3 answers
I have a function in my file ~/.vimrc
that I am using to communicate with my save program
fun! CleanExtraSpaces()
let save_cursor = getpos(".")
let old_query = getreg('/')
:%s/\s\+$//e
call setpos('.', save_cursor)
call setreg('/', old_query)
endfun
com! Cls :call CleanExtraSpaces()
" auto clean trailing spaces
if has("autocmd")
autocmd BufWritePre *.txt,*.js,*.py,*.wiki,*.sh :call CleanExtraSpaces()
endif
This code gets rid of all exta whitespace during save, or you can invoke it manually by typing :Cls<Enter>
the most important part is :%s/\s\+$//e
\s\+ .............. one space or mor
$ ................. at the end of the line
e ................. if not exists any extra space it ignores error messages
+1
source to share