(formatting and some rewording) |
m (→Comments: clarification) |
||
Line 89: | Line 89: | ||
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 <s>(the one currently in the tip)</s> is worthwhile. [[User:JohnBeckett|JohnBeckett]] 08:58, 5 March 2009 (UTC) |
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 <s>(the one currently in the tip)</s> is worthwhile. [[User:JohnBeckett|JohnBeckett]] 08:58, 5 March 2009 (UTC) |
||
---- |
---- |
||
− | :The solution in vimrc_example.vim is exactly the same as {{help|line}}, except that the autocmd is styled across lines instead of concatenated into a single line. [[User:Andres.p|Andres.p]] 18:07, December 1, 2010 (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. [[User:Andres.p|Andres.p]] 18:07, December 1, 2010 (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. [[User:Andres.p|Andres.p]] 17:45, December 1, 2010 (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. [[User:Andres.p|Andres.p]] 17:45, December 1, 2010 (UTC) |
Revision as of 21:53, 22 January 2011
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 a somewhat improved version of the example given for :help line().
Additional Information
- Help for viminfo is at: :help viminfo.
- Part of this tip has made its way into the manual: :help last-position-jump.
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 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)