6

I have the following autocommand, which basically strips whitespace at the end of each line. Unfortunately, at each save, it inserts a step into the undo to jump to the beginning to the file, which is quite annoying. Is there a way to make vim ignore jumping around in the following command, so that undoing keeps the cursor in position?

autocmd BufWritePre *
    \ let s:bufwritepre_currline = line('.') |
    \ let s:bufwritepre_currcol = col('.')   |
    \ silent %s/\s*$//                       |
    \ call cursor(s:bufwritepre_currline, s:bufwritepre_currcol)
  • 1
    Unrelated to the question, but for restoring cursor position, winsaveview()/winrestview() is probably preferable over the line()/col()/cursor() solution given in your example. – Søren Løvborg May 15 '11 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.