Search code examples
functionvimautocmd

Run the CommandTFlush command when a new file is written


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


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.