Question

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? ;)

Was it helpful?

Solution

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top