是否可以让vim标记记住当前窗口中该行的相对位置?

kMa*_*ter 6 vim

我正在使用http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to- 中提出的解决方案的(修改版本)your-file/自动为我的源代码创建和更新标题。

如上述页面所述,在调用 中的write命令时vim,将执行以下命令序列:

  1. 在文件的当前位置设置了一个标记。
  2. “上次修改”字段已更新。这会将光标移动到文件的开头(搜索和替换发生的地方)。
  3. 光标返回到先前标记的位置。

这很好,但有一个有点烦人的问题:假设我们正在编辑靠近窗口底部的一行。如果此时我们保存文件,由于光标移动(用于更新标题),我们正在编辑的行会​​跳转,因此它位于窗口的中间。

据我了解,'a将光标移动到标记标记的位置a并调整窗口内容,使当前行出现在窗口中间。我想知道是否有办法让“标记”也记住窗口中标记线的确切相对位置,并在跳回标记时保持这个位置?

phd*_*phd 4

它在文档中:恢复光标位置

\n\n
   :map <F2> msHmt\xe2\x80\xa6\'tzt`s\n
Run Code Online (Sandbox Code Playgroud)\n\n

(我用省略号跳过了不相关的部分)。

\n\n
ms  store cursor position in the \'s\' mark\nH   go to the first line in the window\nmt  store this position in the \'t\' mark\n
Run Code Online (Sandbox Code Playgroud)\n\n

打破恢复位置:

\n\n
\'t  go to the line previously at the top of the window\nzt  scroll to move this line to the top of the window\n`s  jump to the original position of the cursor\n
Run Code Online (Sandbox Code Playgroud)\n