History Report a problem
Article Edit this page Discussion

Restore cursor to file position in previous editing session

From Vim Tips Wiki

Jump to: navigation, search

Tip 80 Previous TipNext Tip

Created: June 15, 2001 Complexity: intermediate Author: Charles E Campbell Minimum version: 6.0 Karma: 518/170 Imported from: Tip#80


Here's 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:

set viminfo='10,\"100,:20,%,n~/.viminfo
au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

If you're on Unix, the viminfo 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'll need to change the "n" suboption to something like:

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

This tip is a somewhat improved version of the example given for :help line().

[edit] Comments

Help for viminfo is at: :he 'viminfo'

'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

I have futher enhanced this slightly, so that if your last known cursor position is on a folded line, automatically open the fold as well.

I also wanted to avoid doing this for files that are in my temporary directory, since chances are the name has been reused at some point and it is a different file. Enter this verbatim in your .vimrc.

" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" DF - Also do not do this if the file resides in the $TEMP directory,
" chances are it is a different file with the same name.
" This comes from the $VIMRUNTIME/vimrc_example.vim file
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ let b:doopenfold = 1 |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ unlet b:doopenfold |
\ exe "normal zv" |
\ endif

I have made a modification: Do not open the fold in the special case of the cursor being on the edge of an increasing fold. So, the fold will not be opened in the following two cases:

  • cursor is on line 1
  • cursor is on the edge of a fold and the foldlevel of the previous line is smaller than that of the current line.

I also created an augroup.

Here's the updated code:

augroup JumpCursorOnEdit
 au!
 autocmd BufReadPost *
 \ if expand("<afile>:p:h") !=? $TEMP |
 \ if line("'\"") > 1 && line("'\"") <= line("$") |
 \ let JumpCursorOnEdit_foo = line("'\"") |
 \ let b:doopenfold = 1 |
 \ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
 \ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
 \ let b:doopenfold = 2 |
 \ endif |
 \ exe JumpCursorOnEdit_foo |
 \ endif |
 \ endif
 " Need to postpone using "zv" until after reading the modelines.
 autocmd BufWinEnter *
 \ if exists("b:doopenfold") |
 \ exe "normal zv" |
 \ if(b:doopenfold > 1) |
 \ exe "+".1 |
 \ endif |
 \ unlet b:doopenfold |
 \ endif
augroup END

Part of this tip has made its way into the manual: :help last-position-jump.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Restore cursor to file position in previous editing session"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation