Vim:如何恢复光标的逻辑和物理位置?

yin*_*7zi 12 vim

打开文件时如何恢复光标的"逻辑"和"物理"位置?

那是:

  1. 光标应位于文件中的最后一条逻辑行上.

  2. 根据vim窗口,光标应位于最后一次物理线上

我注意到这篇文章.它确实将光标放在右边的逻辑行上.但是窗口中的光标物理位置是第一行还是中间.

UPDATE:该解决方案是mkviewloadview作为由@sehe指出.

为了使它适用于其他插件(在我的情况下,乳胶文件+ 乳胶盒),以下将是有用的:

au BufWinLeave *.tex mkview

au VimEnter *.tex loadview

来自Vim的文档VimEnter:

  • 完成所有启动工作后,包括加载.vimrc文件,执行"-c cmd"参数,创建所有窗口并在其中加载缓冲区.

UPDATE2:更好地组织"view-snapshot-files"

通过创建文件夹~/.vim/view,您将保留所有"view-snapshot-files".

如果您使用的git~/.vim跨计算机同步,也许您愿意

  • 忽略文件~/.vim/view,
  • 但请将空文件夹保留在您的仓库中.

然后你需要(根据这里的答案添加)

  • 创建一个空文件: ~/.vim/view/.gitignore
  • 放入view/*,!.gitignore 放入~/.vim/.gitignore

seh*_*ehe 24

好消息,:mkview已经有了这个(参见下面的文档摘录).

最具体的是:loadview恢复滚动位置,以及折叠状态,如果viewoptions包括cursor,folds.

更好的消息是,如果您愿意,可以透明地为所有打开的文件启用视图.例如,要为所有C源文件启用视图保存,请将其添加到$ MYVIMRC:

au BufWinLeave *.c mkview
au BufWinEnter *.c silent loadview
Run Code Online (Sandbox Code Playgroud)

编辑根据Hongying的评论,结合某些插件,如果使用VimEnterauto命令加载视图,它可能会更好.

(可选)使用该viewdir选项定义已保存视图的位置.

一定要看一下,:mksession因为它更强大,因为它可以恢复多个窗口,标签及其位置,映射,寄存器,选项,折叠状态等.

这个怎么运作

Vim :mkview保存ex命令以恢复位置,如下所示:

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
Run Code Online (Sandbox Code Playgroud)

:loadview 只需获取这些命令,就像任何vimscript一样.

来自文档

注意这是从文档剪切,请确保阅读更多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.
Run Code Online (Sandbox Code Playgroud)

  • 我找到了一个解决方案:改为"au BufWinLeave*.tex mkview和au VimEnter*.tex loadview". (3认同)