Vim scripting: Preserve cursor position and screen view in function call

前端 未结 5 907
庸人自扰
庸人自扰 2021-02-02 11:11

I have some Vim functions that make changes to the document format. When I call this function, I currently use something like the following to save and restore my cursor positio

5条回答
  •  庸人自扰
    2021-02-02 11:27

    You can save a mark for the first on-screen line that is displayed in the window and restore that as well. An example that executes a g? command on the whole buffer and restores both positions:

    :noremap  mkHmlggg?G`lzt`k
    

    Walking through the command:

    • mk: set mark k for the current position
    • H: go to the first on-screen line
    • ml: set mark l for the this position
    • ggg?G: execute the command
    • ``l: jump to markl`
    • zt: set this line the first on-screen line
    • ``k: jump to markk`

提交回复
热议问题