vim:每当缓冲区更改时运行一些代码

4
我希望能够捕获所有修改vim缓冲区的事件,以便记录并将其发送到服务器。我想在插入模式下逐个字符地捕获事件,并在执行pdd等命令时得到通知---即文档发生更改的任何时间。

你想要实现什么目标?也许ttyrec可以帮到你。 - sehe
你是在寻找一个键盘记录器吗?还是需要更多的功能? - johnsyweb
我想制作一个多人协同编辑器(类似于Etherpad的功能)的vim插件。 - nornagon
3个回答

6
在Vim 8中,这变得更加容易了。只需关注以下两个自动命令事件:
TextChanged
TextChangedI

3

浏览了事件列表后,我选择了以下内容:

|BufFilePre|            before changing the name of the current buffer
|BufFilePost|           after changing the name of the current buffer

|FileChangedShell|      Vim notices that a file changed since editing started
|FileChangedShellPost|  After handling a file changed since editing started

|InsertEnter|           starting Insert mode
|InsertChange|          when typing <Insert> while in Insert or Replace mode
|InsertLeave|           when leaving Insert mode

|QuickFixCmdPre|        before a quickfix command is run
|QuickFixCmdPost|       after a quickfix command is run

虽然我没有尝试过,但我相信 Insert* 事件包括像 delete、paste、change 等命令。你还应该查找 |autocmd-events| 中的“Writing”事件。


我现在只是好奇是否可以重新映射默认的vim命令,例如所谓的“dd”和“p” ;) - freitass
这是可能的,但需要付出巨大的努力...而且可能与vimrc不兼容。还需要处理所有变化,例如'2dd'、'vjjp'、'd3k'等。此外,在插入模式下输入文本时,没有任何Insert*方法会触发——只有在进入或离开插入模式时才会触发。 - nornagon

0
一个朋友向我介绍了 Vim 中名字很糟糕的 netbeans 模块。这看起来就是我想要的东西。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接