I am trying to write a vim function to toggle the fzf window if it is already open using fzf.vim.

The use case is if I am in the :Buffers view and I want to change to the :Files view if I try to execute :Files before closing :Buffers then fzf.vim complains.

The code is as below:

function FzfRunning()
    let buffers = filter(range(1, bufnr('$')), 'bufname(v:val) =~# ";#FZF"')
    for i in buffers
        " Delete buffer by ID
        execute "bw!" . i
    endfor
endfunction

command! -nargs=* ToggleFiles call FzfRunning| Files
command! -nargs=* ToggleBuffers call FzfRunning| Buffers

This works except for the fact that the fzf buffer loses focus. I would like it to focus the fzf buffer when ToggleFiles or ToggleBuffers is called.

  • Something like execute 'bdelete ' . bufname(';#FZF')? (maybe you'll have to change ;#FZF to the actual name of a fzf buffer) – statox Dec 29 '16 at 13:33
  • :function! – analogously to :command! – enables you to overwrite an existing function definition of the same name without restarting vim. Useful during vimscript dev/ debugging, as you seem to be doing. – Aaron Thoma Jan 1 '17 at 10:59
  • You selected the global-command tag. That refers specifically to the :g (alias :global) command, thus is not applicable here. (I submitted an edit to remove it.) – Aaron Thoma Jan 1 '17 at 11:10
  • command! -nargs=* only makes sense if you use the arguments (if I'm not mistaken); in this case probably by passing them on: command! -nargs=* ToggleFiles call FzfRunning() | Files <args> – Aaron Thoma Jan 1 '17 at 11:14

I solved this problem with adding sleep

function FzfRunning()
  let buffers = filter(range(1, bufnr('$')), 'bufname(v:val) =~# ";#FZF"')
  for i in buffers
    " Delete buffer by ID
    execute 'bw! ' . i
  endfor
  " we need to sleep here so fzf will be focused properly
  sleep 10m
endfunction

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.