Run the CommandTFlush command when a new file is written
Asked Answered
C

2

6

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

Ca answered 15/8, 2010 at 8:44 Comment(3)
What ks| at the start of your autocommand is doing? Cannot find it in vim help.Evolutionary
Found it. Why do you want to mark your position if your function does not move the cursor?Evolutionary
No idea really. It's just a bad copy/paste job based on some pointers I got in #[email protected]. Haven't quite figured out how to get something helpful out of the help files yet. I'm on my third day with Vim here ;)Ca
E
2
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.

Evolutionary answered 15/8, 2010 at 9:59 Comment(0)
U
17

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
Ultrastructure answered 26/4, 2011 at 14:19 Comment(1)
That works very well. I just stuck that in my .vimrc and now CommandT works as I'd expect it to. Thanks.Rockbottom
E
2
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.

Evolutionary answered 15/8, 2010 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.