写入新文件时运行 CommandTFlush 命令
我试图让 Vim 在写入新文件时运行命令“CommandTFlush”。对于那些不使用 Command-T 插件的人,“CommandTFlush”命令用于重建当前目录中的文件索引。
我想要做的是在文件写入磁盘后运行命令,以便 CommandTFlush 找到该文件并将其添加到其索引中。
我尝试自己编写一个函数,但它要么不触发,要么触发得太快(在写入文件之前,重点是将文件添加到索引中):
au! BufWritePre * ks| call NewFilesUpdatesCommandT()
function! NewFilesUpdatesCommandT()
let filename=@%
if !filereadable(filename)
CommandTFlush
endif
endfunction
我怀疑它可以通过设置来解决如果文件刚刚创建,则在 BufWritePre 中执行一些布尔变量(isTheFileNew),然后在 BufWritePost 中执行 CommandTFlush 命令,但我无法弄清楚语法。另一个解决方案可能是从 BufWritePre 回调中设置/取消设置 BufWritePost 回调,如果可能的话......
有人可以帮我吗? ;)
I'm trying to make Vim run the command 'CommandTFlush' whenever a new file is writte. For those not using the Command-T plugin, the 'CommandTFlush' command is used to rebuild an index of files in the current directory.
What I want to do is run the command after the file is written to disk, so that CommandTFlush will find the file and add it to it's index.
I've tried writing a function myself, but either it doesn't fire or it fires too soon (before the file is written, and the whole point is to add the file to the index):
au! BufWritePre * ks| call NewFilesUpdatesCommandT()
function! NewFilesUpdatesCommandT()
let filename=@%
if !filereadable(filename)
CommandTFlush
endif
endfunction
I suspect it could be solved by setting some boolean var (isTheFileNew) in BufWritePre and then execute the CommandTFlush command in BufWritePost if the file was just created, but I can't figure out the syntax. Another solution could be setting/unsetting the BufWritePost callback from within BufWritePre callback, if that's possible...
Could anybody help me out here? ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的解决方案。每当写入文件以及 Vim 窗口获得焦点时,它都会触发 CommandTFlush。当您在 vim 之外创建文件时,这非常有用 - 例如通过在版本控制系统中的分支之间切换。重新进入 Vim 后,新文件将立即在 CommandT 中可用。
Here is my solution. It triggers CommandTFlush whenever a file is written and also whenever Vim's window gains focus. This is useful when you create files outside of vim - for example by switching between branches in your version control system. The new files will be available in CommandT immediately after you re-enter Vim.
这是你的第二个建议的实现。
This is a realization of your second suggestion.