Tip #80: Restore cursor to file position in previous editing session
tip karma |
Rating 661/213, Viewed by 11161
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
June 15, 2001 6:47 |
|
complexity: |
|
intermediate |
author: |
|
Charles E Campbell |
|
as of Vim: |
|
6.0 |
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:
Ex. 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
Ex. set viminfo='10,\"100,:20,%,nc:\\some\\place\\under\\Windoz\\_viminfo
This tip is a somewhat improved version of the example given for :he line()
in the Vim on-line documentation.
<< How to use :grep to get a clickable list of function names |
Substitution of characters and lines in VIM is made far easier with the s and S commands >>
Additional Notes
[email protected],
May 20, 2004 14:52
|
could you explain all the option of the viminfo variable ?
'x = remember the cursor position for the last x files edited ?
right ?
"x = remember last x lines in the buffer.
is it possible to have a list of theses ? can't find them :(
thanks
|
[email protected] - NOSPAM,
June 21, 2004 7:43
|
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
|
David Fishburn,
September 23, 2004 19:12
|
Thanks to Bram, 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 changes are the name has been reused at some point and it is a different file.
Enter this verbatim in your .vimrc.
" From Bram:
" 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
|
Suresh Govindachar,
September 24, 2004 19:57
|
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:
1) cursor is on line 1
2) 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
--Suresh
|
|