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?

  • Obligatory article: Oil and vinegar - split windows and the project drawer. – Peter Rincker May 18 '18 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 '18 at 17:12

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('%')
    exe 'b ' . bufnum
  if exists('g:launching_fzf') | unlet g:launching_fzf | endif
  • 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 '18 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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