Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid moving cursor on autcmd BufWritePre in .vimrc

Tags:

vim

I have the following line in my .vimrc.

autocmd BufWritePre * silent! v/\_s*\S/d

This strips empty lines at the ends of files, on save. The problem is, when it strips the lines it also moves the cursor to the last line of the file. Is there anyway to avoid modifying the cursor location?

like image 413
Blake Taylor Avatar asked Sep 08 '12 06:09

Blake Taylor


1 Answers

function! <SID>DelEmptyLinesEnd()
    let l = line(".")
    let c = col(".")
    v/\_s*\S/d
    call cursor(l, c)
endfunction

autocmd BufWritePre * :call <SID>DelEmptyLinesEnd()
like image 93
eminor Avatar answered Nov 04 '22 04:11

eminor