Vim Tips Wiki
Advertisement
Tip 80 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Charles E Campbell · version 6.0


Here is something for your vimrc which will allow you to restore your cursor position in a file over several editing sessions. This technique uses the viminfo option, so be sure to have viminfo enabled with reasonable options (it is enabled by default):

" Tell vim to remember certain things when we exit
"  '10  :  marks will be remembered for up to 10 previously edited files
"  "100 :  will save up to 100 lines for each register
"  :20  :  up to 20 lines of command-line history will be remembered
"  %    :  saves and restores the buffer list
"  n... :  where to save the viminfo files
set viminfo='10,\"100,:20,%,n~/.viminfo

If you are on Unix, the viminfo example above is probably fine as is (but check up on Vim's help for viminfo to see if you like the settings above). For Windows you will need to change the "n" suboption to something like:

set viminfo='10,\"100,:20,%,nc:\\some\\place\\under\\Windows\\_viminfo

Following that, add the main function that restores the cursor position and its autocmd so that it gets triggered:

function! ResCur()
  if line("'\"") <= line("$")
    normal! g`"
    return 1
  endif
endfunction

augroup resCur
  autocmd!
  autocmd BufWinEnter * call ResCur()
augroup END

Optionally add a function that takes care of selectively unfolding right after restoring the cursor position:

if has("folding")
  function! UnfoldCur()
    if !&foldenable
      return
    endif
    let cline = line(".")
    if cline > 1
      let cfold = foldlevel(cline)
      let ufold = foldlevel(cline - 1)
      if ufold > 0
        execute "normal!" (cfold > ufold ? ufold : cfold) . "zo"
        return 1
      endif
    endif
  endfunction
endif

And modify the augroup:

augroup resCur
  autocmd!
  if has("folding")
    autocmd BufWinEnter * if ResCur() | call UnfoldCur() | endif
  else
    autocmd BufWinEnter * call ResCur()
  endif
augroup END

This tip is an improved version of the example given for :help last-position-jump. It fixes a problem where the cursor position will not be restored if the file only has a single line.

Additional Information

Comments

 TO DO 

  • Check my comment following, and the explanation after that (refactored from Anres.p's comment with dealt-with material omitted).
  • I have not yet looked at the current solution. JohnBot 07:14, January 22, 2011 (UTC)

On my system, I copy the restore-last-position code from vimrc_example.vim (a file distributed with Vim). That code is the basis of the first solution in the tip (now deleted). vimrc_example also makes the help 'last-position-jump' obsolete. That first solution deserves deletion IMHO because it has a totally inexplicable "norm $" that doesn't seem at all useful to me.

My current feeling is that vimrc_example should be explained, and perhaps the second solution (currently deleted) should be shown. I'm not at all sure that the third solution (the one currently in the tip) is worthwhile. JohnBeckett 08:58, 5 March 2009 (UTC)


The solution in vimrc_example.vim is exactly the same as :help line and :help last-position-jump, except that the autocmd is styled across lines instead of concatenated into a single line. Andres.p 18:07, December 1, 2010 (UTC)
The reason why the code in vimrc_example.vim shouldn't be explained I already gave in a previous comment that was deleted by the bot. :) It performs a redundant conditional that keeps from restoring the column position when the file is one line long... Andres.p 21:55, January 22, 2011 (UTC)
This is exactly why we should mention the version in the :help. We need to know why the :help version can't just be used as-is. I personally have not paid much attention to this tip exactly because I have thought the version in the :help to be enough for my needs. --Fritzophrenic 15:53, January 24, 2011 (UTC)

The particular issue it solves is the same as the original tip, which is unfolding just so that the previous cursor prosition ('") is unfolded. It's almost the same as :normal! zv, except that if ('") happens to be the start of a new folding section, it does not get unfolded; the upper line does. I'll eventually get around wording this in comprehensible way. Andres.p 17:45, December 1, 2010 (UTC)

Advertisement