写入新文件时运行 CommandTFlush 命令

发布于 2024-09-14 02:06:20 字数 629 浏览 2 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知在何时 2024-09-21 02:06:20

这是我的解决方案。每当写入文件以及 Vim 窗口获得焦点时,它都会触发 CommandTFlush。当您在 vim 之外创建文件时,这非常有用 - 例如通过在版本控制系统中的分支之间切换。重新进入 Vim 后,新文件将立即在 CommandT 中可用。

augroup CommandTExtension
  autocmd!
  autocmd FocusGained * CommandTFlush
  autocmd BufWritePost * CommandTFlush
augroup END

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.

augroup CommandTExtension
  autocmd!
  autocmd FocusGained * CommandTFlush
  autocmd BufWritePost * CommandTFlush
augroup END
桜花祭 2024-09-21 02:06:20
augroup NFUCT
    autocmd!
    autocmd BufWritePre * call NFUCTset()
augroup END
function NFUCTset()
    if !filereadable(expand('%'))
        augroup NFUCT
            autocmd BufWritePost * call NFUCT()
        augroup END
    endif
endfunction
function NFUCT()
    augroup NFUCT
        autocmd!
        autocmd BufWritePre * call NFUCTset()
    augroup END
    CommandTFlush
endfunction

这是你的第二个建议的实现。

augroup NFUCT
    autocmd!
    autocmd BufWritePre * call NFUCTset()
augroup END
function NFUCTset()
    if !filereadable(expand('%'))
        augroup NFUCT
            autocmd BufWritePost * call NFUCT()
        augroup END
    endif
endfunction
function NFUCT()
    augroup NFUCT
        autocmd!
        autocmd BufWritePre * call NFUCTset()
    augroup END
    CommandTFlush
endfunction

This is a realization of your second suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文