Vim:如何恢复光标的逻辑和物理位置? [英] Vim: how to restore the cursor's logical and physical positions?

查看:32
本文介绍了Vim:如何恢复光标的逻辑和物理位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

就是:

  1. 光标应该在文件的最后一个逻辑行上.

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

根据vim窗口,光标应该在最后一次物理行上

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.

更新:如@sehe 所述,解决方案是 mkviewloadview.

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

来自 VimEnter 的 Vim 文档:

From the Vim documentation of VimEnter:

  • 在完成所有启动工作后,包括加载 .vimrc 文件、执行-c cmd"参数、创建所有窗口并加载其中的缓冲区.
  • 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:为了更好地组织视图快照文件"

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

通过创建一个文件夹~/.vim/view,你将把所有的view-snapshot-files"保存在那里.

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

如果你正在使用 git 来同步 ~/.vim 跨计算机,也许你想

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

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

然后你需要(根据答案添加此处)

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

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

推荐答案

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

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

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

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

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

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

编辑 根据红英的评论,结合某些插件,如果您使用 VimEnter 自动命令加载视图可能会更好.

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.

可以选择使用 viewdir 选项来定义保存视图的位置.

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

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

Vim :mkview 保存 ex 命令恢复位置如下:

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 只是获取这些命令,就像任何 vimscript 一样.

:loadview just sources those commands, like any vimscript.

注意这是从文档中剪辑,确保阅读更多he :mkview

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.

这篇关于Vim:如何恢复光标的逻辑和物理位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆