(prevent error when $ > .) |
No edit summary |
||
Line 23: | Line 23: | ||
function! s:ResCur() |
function! s:ResCur() |
||
− | + | if line("'\"") <= line("$") |
|
⚫ | |||
− | |||
⚫ | |||
− | return |
||
endif |
endif |
||
+ | endfunction |
||
+ | |||
+ | function! s:UnfoldCur() |
||
⚫ | |||
if pline > 1 && &foldenable |
if pline > 1 && &foldenable |
||
Line 35: | Line 37: | ||
if ufold > 0 |
if ufold > 0 |
||
− | + | execute (pfold > ufold ? uline : "") . "normal! zv" |
|
− | execute uline |
||
⚫ | |||
− | |||
− | execute "normal! zv" |
||
endif |
endif |
||
endif |
endif |
||
− | |||
⚫ | |||
endfunction |
endfunction |
||
augroup jumpCursor |
augroup jumpCursor |
||
autocmd! |
autocmd! |
||
+ | if has("folding") |
||
⚫ | |||
+ | autocmd BufWinEnter * call s:ResCur() | call s:UnfoldCur() |
||
+ | else |
||
⚫ | |||
⚫ | |||
augroup END |
augroup END |
||
</pre> |
</pre> |
Revision as of 14:36, 28 November 2010
created June 15, 2001 · complexity intermediate · author Charles E Campbell · version 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:
" 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 function! s:ResCur() if line("'\"") <= line("$") execute "normal! g`\"" endif endfunction function! s:UnfoldCur() let l:pline = line(".") if pline > 1 && &foldenable let l:uline = pline - 1 let l:ufold = foldlevel(uline) let l:pfold = foldlevel(pline) if ufold > 0 execute (pfold > ufold ? uline : "") . "normal! zv" endif endif endfunction augroup jumpCursor autocmd! if has("folding") autocmd BufWinEnter * call s:ResCur() | call s:UnfoldCur() else autocmd BufWinEnter * call s:ResCur() endif augroup END
If you're 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'll need to change the "n" suboption to something like:
set viminfo='10,\"100,:20,%,nc:\\some\\place\\under\\Windows\\_viminfo
This tip is a somewhat improved version of the example given for :help line().
Additional Information
- Help for viminfo is at: :he 'viminfo'
- Part of this tip has made its way into the manual: :help last-position-jump.
Comments
TO DO
- I have cleaned up the nonstandard formatting given to the tip.
- Someone probably needs to restore at least part of the original tip, per my next comment.
- The most complex of the proposed solutions from the original tip has been kept. Since it was the last such solution, there is a reasonable hope that it would be the best. But I don't think it is. As noted (in "Limitations") it doesn't restore the column position. That's by design! It uses "exe <linenumber>" to restore the position, and so is not satisfactory IMHO.
- I haven't taken the time to understand all that fold stuff. I suppose it would be useful to some, so I'm reluctant to delete it. But it's frustrating how, unless you are familiar with the issue that the author is addressing, you would need to study the tip for ages to work out why it works like this, and what it actually does. Use of "JumpCursorOnEdit_foo" as a variable name is a worry. The exe "+".1 is just exe "+1".
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)
Personally, I think the example given in :help line() is fine. It does just what I want. --xaprb
The help example mentioned above is:
This autocommand jumps to the last known position in a file just after opening it, if the '"' mark is set: :au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
Altering the vimtip version to use g` instead of g' made it return to the line and character instead of just the line eg
:au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
--bristley
This is from an old gentoo vimrc. It mostly works, I add it because it's much simpler than the one above, but I don't understand either really:
" When editing a file, always jump to the last cursor position autocmd BufReadPost * \ if ! exists("g:leave_my_cursor_position_alone") | \ if line("'\"") > 0 && line ("'\"") <= line("$") | \ exe "normal g'\"" | \ endif | \ endif
--jamax