vim - 替换缓冲区时防止光标跳到底部

标签 vim vim-plugin

我将内容存储在一个变量 ( out ) 中,我想用当前缓冲区替换它。我目前正在这样做(简化版):

let splitted = split(out, '\n')
if line('$') > len(splitted)
    execute len(splitted) .',$delete'
endif
call setline(1, splitted)

(详细信息:https://github.com/fatih/vim-go/blob/master/autoload/go/fmt.vim#L130)

然而setline()这里会导致某些机器运行缓慢和 https://github.com/fatih/vim-go/issues/459 .我自己分析过,但对我来说 setline 不是问题。无论如何,我需要一个更快的解决方案。所以我想出了其他几个解决方案。

第一个是,将输出放入寄存器,删除所有行,然后将其放回:
let @a = out
% delete _
put! a
$ delete _

第二种解决方案是使用 append() (之前在 vim-go https://github.com/fatih/vim-go/commit/99a1732e40e3f064300d544eebd4153dbc3c60c7 中使用过):
let splitted = split(out, '\n')
%delete _
call append(0, splitted)
$delete _

他们两者 工作!然而,它们也会引起我仍然无法解决的副作用,并且也写在标题中。问题描述如下:

If a buffer is opened in another view (say next to next), and we call one of the two solutions above, it breaks the cursor of the other view and jumps to the bottom



这是一个更好地展示它的 GIF(每当我调用 :w 上面的一个程序时):http://d.pr/i/1buDZ

有没有办法替换缓冲区的内容,它既快又不破坏布局?或者我如何通过上述程序之一来防止它?

谢谢。

最佳答案

你试了吗winsaveview()winrestview() ?

:let old_view=winsaveview()
:% delete _
:put! =out
:$ delete _
:call winrestview(old_view)

但是我对以更快的方式粘贴文本一无所知

关于vim - 替换缓冲区时防止光标跳到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356643/

相关文章:

Vim:从光标删除到下一个句点

vim - 如何让 VIM matchit 插件与 ColdFusion 和 HTML 一起使用?

javascript - 使用变量默认占位符定义 UltiSnip

vim - 在 vim 中使用 Doxygen 风格的注释

visual-studio - 如何在 Visual Studio 2010 中使用自由文本(未预定义)对所选单词执行 "Surround-with",例如在 Vim 中

Vim 分割线命令

vim - Vundle插件卸载后仍然存在

go - 安装 nvim-go 失败并显示 "Undefined variable: g:go#debug"

vim - 为 VIM 添加 GoLang 语法高亮

vim - 如何在 VIM 中显示搜索结果出现的滚动条上启用突出显示?