Vim 脚本:在函数调用中保留光标位置和屏幕视图 [英] Vim scripting: Preserve cursor position and screen view in function call

查看:26
本文介绍了Vim 脚本:在函数调用中保留光标位置和屏幕视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 Vim 函数可以更改文档格式.当我调用这个函数时,我目前使用类似以下的东西来保存和恢复我的光标位置:

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

func! Foo()
  :norm mz
  ...
  :norm `z
endf

这正确地保存了光标位置,但有时会改变窗口位置,使当前行从靠近屏幕顶部移动到靠近底部,反之亦然.有没有办法同时保留光标位置和行的屏幕位置?

This properly saves the cursor position, but it sometimes changes the window position, so that the current line moves from being near the top of the screen to being near the bottom or vice versa. Is there a way to preserve both cursor position and the on-screen position of the line?

推荐答案

let l:winview = winsaveview()
" do stuff
call winrestview(l:winview)

这应该几乎完全符合您的要求,可能除了光标上方的行数发生变化(我怀疑光标上方已删除的行会导致光标向下移动).

This should pretty much do exactly what you want it to do, possibly excepting the line count changing above the cursor (I suspect that deleted lines above the cursor would have the effect of moving the cursor down).

这篇关于Vim 脚本:在函数调用中保留光标位置和屏幕视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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