Fzf: How can i prevent that fzf open files inside NERDtree buffer?

Created on 21 Dec 2015  路  19Comments  路  Source: junegunn/fzf

How can i prevent that fzf open files inside neotree buffer?
I am always do that, find files when neotree buffer is on focus :(

fzf

thanks

neovim question

Most helpful comment

Hmm, you'll probably have to use a custom mapping that moves the focus if the cursor is on that window. You mean nerdtree, right? Something like this:

nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":FZF\<cr>"

All 19 comments

Hmm, you'll probably have to use a custom mapping that moves the focus if the cursor is on that window. You mean nerdtree, right? Something like this:

nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":FZF\<cr>"

OH Yeah, i mean nerdtree, neotree is a emacs thing :p,
This map works like a charm :) 馃弲 馃帠
Thank you so much! and also thanks for the great job of your plugins and themes i love it 馃挮 馃馃憤 .

Closing issue in 4...3...2...

Sorry, your command doesn't work for me.
I'm using newest version of both NerdTree and FZF, this problem still happens...

nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":FZF\<cr>"

@mrleolink Works for me. It's probably something in your configuration; some plugin or your own autocmd. Disable everything but fzf and nertree and see if the problem persists.

@junegunn Ok, so I tried to disable everything but the problem still persists.

I'm using neovim, this is my init.vim

execute pathogen#infect()
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

"fzf: https://github.com/junegunn/fzf
set rtp+=$FZF_HOME
map <c-p> :FZF<CR>
autocmd! FileType fzf tnoremap <buffer> <leader>q <c-p>
nnoremap <silent> <expr> <Leader><Leader> (expand('%') =~ 'NERD_tree' ? "\<c-w>\<c-w>" : '').":FZF\<cr>"

As you can see, I'm using pathogen to load nerdtree. I don't have anything else in my bundle folder.

I suspect you don't fully understand the lines. The suggested line is a mapping of its own and not a global configuration so it does not affect <c-p> binding you have above. Change <leader><leader> to <c-p>.

It works!!!
Thank you 馃憤

For anyone still looking for a solution I created the following function and put it in my .vimrc, it will also check the number of buffers and if you open vim like this vim . it will allow you to use your FZF mapping to open a file instead of the nerdtree browser:

function! FZFOpen(command_str)
  if (expand('%') =~# 'NERD_tree' && winnr('$') > 1)
    exe "normal! \<c-w>\<c-w>"
  endif
  exe 'normal! ' . a:command_str . "\<cr>"
endfunction

nnoremap <silent> <C-b> :call FZFOpen(':Buffers')<CR>
nnoremap <silent> <C-g>g :call FZFOpen(':Ag')<CR>
nnoremap <silent> <C-g>c :call FZFOpen(':Commands')<CR>
nnoremap <silent> <C-g>l :call FZFOpen(':BLines')<CR>
nnoremap <silent> <C-p> :call FZFOpen(':Files')<CR>

My comment has a bigger scope than this issue, but it is still related and this might interest some people. I currently have a more advanced setup that prevents opening any file in NERDTree when NERDtree is focused:

  • files opened with FZF
  • files opened with mappings, for example my VIMRC or the project README
  • files opened with :edit
  • etc.

Plus, if you change your mind and exit FZF, you still have focus on the original NERDTree. You don't need to <c-w><c-w> back, and you don't need wrapper functions.

Here is the code:


let g:fzf_layout = { 'window': 'let g:launching_fzf = 1 | keepalt topleft 100split enew' }

autocmd FileType nerdtree let t:nerdtree_winnr = bufwinnr('%')
autocmd BufWinEnter * call PreventBuffersInNERDTree()

function! PreventBuffersInNERDTree()
  if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree'
    \ && exists('t:nerdtree_winnr') && bufwinnr('%') == t:nerdtree_winnr
    \ && &buftype == '' && !exists('g:launching_fzf')
    let bufnum = bufnr('%')
    close
    exe 'b ' . bufnum
  endif
  if exists('g:launching_fzf') | unlet g:launching_fzf | endif
endfunction

It's kind of hacky because I do "code injection" by adding the g:launching_fzf variable inside the layout configuration. There may be a more elegant solution but in the meantime it works and I didn't have to change the FZF plugin source code.

let g:fzf_layout = { 'window': 'let g:launching_fzf = 1 | keepalt topleft 100split enew' }

autocmd FileType nerdtree let t:nerdtree_winnr = bufwinnr('%')
autocmd BufWinEnter * call PreventBuffersInNERDTree()

function! PreventBuffersInNERDTree()
  if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree'
    \ && exists('t:nerdtree_winnr') && bufwinnr('%') == t:nerdtree_winnr
    \ && &buftype == '' && !exists('g:launching_fzf')
    let bufnum = bufnr('%')
    close
    exe 'b ' . bufnum
  endif
  if exists('g:launching_fzf') | unlet g:launching_fzf | endif
endfunction

Does this solution still works correctly for anybody?
Have problems with using it:

Error detected while processing function PreventBuffersInNERDTree:
line    5:
E444: Cannot close last window

try this:

au BufEnter * if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree' && winnr('$') > 1 | b# | exe "normal! \<c-w>\<c-w>" | :blast | endif

try this:

au BufEnter * if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree' && winnr('$') > 1 | b# | exe "normal! \<c-w>\<c-w>" | :blast | endif

Thanks!, it's all I wanted

try this:

au BufEnter * if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree' && winnr('$') > 1 | b# | exe "normal! \<c-w>\<c-w>" | :blast | endif

@danielgatis weird things happen when running with neovim 0.4, this automatic command still working?

Original layout without fzf command:

image

After :Files command, nerdtree at bottom, fzf command at top lef and empty buffer at top right:

image

(everything broken at this point)

For a future reference: if you are using vim-nerdtree-tabs plugin and want fzf to open in another window (rather than inside the 'pinned' NERDTree buffer), the following would work:

if exists('t:NERDTreeBufName') && bufname('%') == t:NERDTreeBufName
  wincmd w   " move the focus outside nerdtree
endif

Note that in this case fzf will open files in the same buffer if it is not a pinned NERDTree buffer (e.g. e .).

Just to add my 2 cents, i used @jeromedalbert solution and worked pretty well for me, the only thing i didn't liked was that it was closing the nerdtree buffer, so i tried to update it in order to keep it open even when i jump to a file.

Here is my solution:

autocmd FileType nerdtree let t:nerdtree_winnr = bufwinnr('%')
autocmd BufWinEnter * call PreventBuffersInNERDTree()

function! PreventBuffersInNERDTree()
  if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree'
    \ && exists('t:nerdtree_winnr') && bufwinnr('%') == t:nerdtree_winnr
    \ && &buftype == '' && !exists('g:launching_fzf')
    let bufnum = bufnr('%')
    close
    exe 'b ' . bufnum
    NERDTree
  endif
  if exists('g:launching_fzf') | unlet g:launching_fzf | endif
endfunction

So basically i just reopened it when the new buffer open in the correct window.
Hope this helps!

I really liked @dkarter's solution, I "improved/customised" it a little, I often have nerdtree on the left, and quickfix window below, so I would like to move a window to the right and up if the buffer is not modifiable or NERDTree or a quickfix window:

" Prevent FZF commands from opening in none modifiable buffers
function! FZFOpen(cmd)
    " If more than 1 window, and buffer is not modifiable or file type is
    " NERD tree or Quickfix type
    if winnr('$') > 1 && (!&modifiable || &ft == 'nerdtree' || &ft == 'qf')
        " Move one window to the right, then up
        wincmd l
        wincmd k
    endif
    exe a:cmd
endfunction

" FZF in Open buffers
nnoremap <silent> <leader><leader> :call FZFOpen(":Buffers")<CR>

" FZF Search for Files
nnoremap <silent> <leader>f :call FZFOpen(":Files")<CR>

" FZF Search for Files in home dir
nnoremap <silent> <leader>~ :call FZFOpen(":Files ~")<CR>

" FZF Search for previous opened Files
nnoremap <silent> <leader>zh :call FZFOpen(":History")<CR>

try this:

au BufEnter * if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree' && winnr('$') > 1 | b# | exe "normal! \<c-w>\<c-w>" | :blast | endif

Thanks!, it's all I wanted too

I like danielgatis solution; however if i look for a file which have already in the buffer. It didn't bring it up

try this:

au BufEnter * if bufname('#') =~ 'NERD_tree' && bufname('%') !~ 'NERD_tree' && winnr('$') > 1 | b# | exe "normal! \<c-w>\<c-w>" | :blast | endif

Works for me, thanks!!!! :love_you_gesture:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aleclarson picture aleclarson  路  3Comments

chrisamow picture chrisamow  路  3Comments

erusev picture erusev  路  3Comments

alistaircolling picture alistaircolling  路  3Comments

natemara picture natemara  路  3Comments