Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
share|improve this question
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

Have you tried upgrading vim? I've just tried that in vim7.3 on ubuntu and it worked fine, stripped all the whitespace and left the cursor in the same place.

share|improve this answer
The problem is not the cursor position, but the fact that it inserts a movement in the undo history. You can see what I mean if you have a longer file (multiple screen pages), run the autocmd, and then undo something. – Dave Vogt Nov 1 '10 at 8:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.