0

so whenever I open a file be it from using FZF, NERDTree or directly using edit, IF I happen to be in the NERDTree buffer it will open the file in the buffer.

How do I make it so that whenever a file is open (no matter how) it will open in the first non NERDTree buffer/window so that NERDTree remains open no matter what?

2
  • 1
    Obligatory article: Oil and vinegar - split windows and the project drawer. May 18, 2018 at 16:18
  • That's interesting, I think I've read that before. My main reason for wanting to know this is singular, I get annoyed when the file opens in the extremely narrow NERDTree buffer view, thus I now have to close the narrow buffer view, open the file in the main view and then IF I need to re-open NERDTree (less of a thing since I use FZF but you get the idea)
    – Thermatix
    May 18, 2018 at 17:12

2 Answers 2

1

The same issue bothered me for a long time.

For fzf there is a discussion about it on github. They came up with some keybindings:

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

but this post is the most promising https://github.com/junegunn/fzf/issues/453#issuecomment-366466757

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.

and this is his 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
1
  • Do you know how to alter keepalt topleft 100split enew so it opens in the bottom? my default layout is \'down': '~20%'
    – Thermatix
    May 22, 2018 at 9:54
0

My setup, for someone who may want to improve NERDTree behavior.

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
    call ToggleNerdTree()
  endif
endfunction

"close" will close origin nerdtree pane, and ToggleNerdTree() will reopen it.

function NotNerdTreePane()
  return bufname('%') !~# 'NERD_tree_' && winnr("$") > 1 && strlen(expand('%')) > 0 && &modifiable && exists("g:NERDTree")
endfunction

function ToggleNerdTree()
  if g:NERDTree.IsOpen() 
    :NERDTreeClose
  else 
    :NERDTreeFind
  endif
endfunction

autocmd BufEnter *  if (NotNerdTreePane() && g:NERDTree.IsOpen()) | NERDTreeFind | wincmd p | endif
autocmd SessionLoadPost * if (NotNerdTreePane() && !g:NERDTree.IsOpen()) | NERDTreeFind | wincmd p | endif 
autocmd VimLeave * NERDTreeClose

NotNerdTreePane() will check current pane and when you enter a buf, it will help to find current file to show in nerdtree.

1
  • 2
    Welcome to Vi and Vim! We encourage answers to accompany code with some explanation of what it's doing, to make it easier for others to follow along.
    – D. Ben Knoble
    Nov 20, 2019 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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