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

时间:2012-01-13 17:04:24

标签: vim

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

那是:

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

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

  3. 我注意到this post。它确实将光标放在右边的逻辑行上。但窗口中的光标物理位置是第一行或中间。

    更新: @sehe指出,解决方案为mkviewloadview

    要使其与其他插件一起使用(在我的情况下,乳胶文件+ latex-box),以下内容将非常有用:

    au BufWinLeave *.tex mkview

    au VimEnter *.tex loadview

    来自VimEnter的Vim文档:

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

    UPDATE2 :更好地组织“view-snapshot-files”

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

    如果您使用git在计算机之间同步~/.vim,也许您想要

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

    然后你需要(根据答案添加here

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

2 个答案:

答案 0 :(得分:24)

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

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

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

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

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

(可选)使用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

: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.

答案 1 :(得分:-5)

刚刚放

  

设置隐藏

进入你的.vimrc