Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: how to restore the cursor's logical and physical positions?

Tags:

vim

How to restore the cursor's "logical" and "physical" positions when I open a file ?

That's:

  1. the cursor should be on the last-time logical line in the file.

  2. the cursor should be on the last-time physical line according to the vim window

I've notice this post. It does put the cursor on the right logical line. But the cursor physical position in the window is the first line or the middle.

UPDATE: The solution is mkview and loadview as noted by @sehe.

To make it work with other plugins (in my case, latex file + latex-box), the following would be useful:

au BufWinLeave *.tex mkview

au VimEnter *.tex loadview

From the Vim documentation of VimEnter:

  • After doing all the startup stuff, including loading .vimrc files, executing the "-c cmd" arguments, creating all windows and loading the buffers in them.

UPDATE2: To better orginize the "view-snapshot-files"

By creating a folder ~/.vim/view, you will keep all the "view-snapshot-files" there.

If you are using git to synchronize ~/.vim across computers, maybe you'd like to

  • ignore the files in ~/.vim/view,
  • but keep the empty folder in your repo.

Then you need to (addapted according to the answers here)

  • create a empty file: ~/.vim/view/.gitignore
  • put view/*, and !.gitignore in ~/.vim/.gitignore
like image 685
ying17zi Avatar asked Jan 13 '12 17:01

ying17zi


1 Answers

Good news, :mkview already has this (see documentation excerpt below).

Most specifically :loadview restores scroll position, as well as folding state, if viewoptions includes cursor,folds.

The even better news is, that you can transparently enable views for all opened files if you wish to. E.g. to enable view saving for all C source-files, add this to $MYVIMRC:

au BufWinLeave *.c mkview
au BufWinEnter *.c silent loadview

Edit As per Hongying's comments, in combination with certain plugins, it might work better if you used the VimEnter auto command for loading the view.

Optionally use the viewdir option to define the location for saved views.

Be sure to also look at :mksession because it is even more powerful, in that it can restore multiple windows, tabs and their positions, mappings, registers, options, fold state etc. etc.

How it works

Vim :mkview saves ex commands to restore the location like the following:

silent! normal! zE
let s:l = 88 - ((4 * winheight(0) + 4) / 9)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
88
normal! 025l

:loadview just sources those commands, like any vimscript.

From the docs

Note This is clipped from the docs, be sure the read more doing he :mkview

                            *:mkvie* *:mkview*
:mkvie[w][!] [file] Write a Vim script that restores the contents of the
            current window.
            When [!] is included an existing file is overwritten.
            When [file] is omitted or is a number from 1 to 9, a
            name is generated and 'viewdir' prepended.  When the
            last directory name in 'viewdir' does not exist, this
            directory is created.
            An existing file is always overwritten then.  Use
            |:loadview| to load this view again.
            When [file] is the name of a file ('viewdir' is not
            used), a command to edit the file is added to the
            generated file.

The output of ":mkview" contains these items:
1. The argument list used in the window.  When the global argument list is
   used it is reset to the global list.
   The index in the argument list is also restored.
2. The file being edited in the window.  If there is no file, the window is
   made empty.
3. Restore mappings, abbreviations and options local to the window if
   'viewoptions' contains "options" or "localoptions".  For the options it
   restores only values that are local to the current buffer and values local
   to the window.
   When storing the view as part of a session and "options" is in
   'sessionoptions', global values for local options will be stored too.
4. Restore folds when using manual folding and 'viewoptions' contains
   "folds".  Restore manually opened and closed folds.
5. The scroll position and the cursor position in the file.  Doesn't work very
   well when there are closed folds.
6. The local current directory, if it is different from the global current
   directory.
like image 72
sehe Avatar answered Oct 22 '22 18:10

sehe