3

If I have many buffers opened and, if the cursor (focus) is within NERDTree, when I change the buffer with :bn or :bp, the content of buffer loads within NERDTree.

There's a way to, before buffer enter, check if cursor is focus on NERDTree and type <c-w><c-w> to prevent buffer loads within NERDTree?

5
  • Something along the lines of autocmd BufUnload nerdtree <your commands here>, maybe. There are a lot of posts that deal with nerdtree specific buffers...maybe not this question exactly but similar enough to reveal some useful information.
    – B Layer
    Feb 13, 2020 at 12:01
  • 1
    Tks @BLayer! I will try solutions and post here if I found something. Feb 13, 2020 at 12:10
  • Sounds good. There will probably be some degree of scripting required, even if it's just the rest of an autocmd line like I mentioned so if you're not experienced with that just try to get a rough idea, come back and update your question with any relevant new details and we'll go from there. (If you are experienced you might figure it all out for yourself.)
    – B Layer
    Feb 13, 2020 at 12:51
  • If you want an easy workaround start using Normal mode mappings to cycle through buffers and use a mapping like this for :bnext ... :nnoremap <silent> <leader>n :if &filetype != 'nerdtree' <bar> :bnext <bar> endif<cr> and similar for :bprev. (I use such mappings for bnext/bprev even without having a requirement like yours...it's quicker!)
    – B Layer
    Feb 21, 2020 at 2:21
  • Did you found a solution to your problem? Apr 8, 2023 at 7:23

2 Answers 2

0

One can use interrupt() to abort event in progress. So for NERDtree one could do something like:

autocmd BufWinLeave * if &filetype == 'nerdtree' | call interrupt() | endif

BufWinLeave

Before a buffer is removed from a window. Not when it's still visible in another window. Also triggered when exiting. It's triggered before BufUnload or BufHidden.

0

Here is the solution I chose:

function! IsSideBar(buf_nr)
" Return 1 if the buffer correspond to a side bar:
" - A terminal window
" - The quickfix window
" - The help
" - The NERDTree side bar
" - ...
  let buf_name = bufname(a:buf_nr)
  let buf_type = getbufvar(a:buf_nr, '&filetype')
  let readonly = getbufvar(a:buf_nr, '&readonly')

  let term_buffers = term_list()

  if buf_type ==# 'qf'
    " QuickFix, LocationList:
    return 1

  elseif buf_type ==# 'help'
    " Help Window:
    return 1

  elseif buf_type ==# 'undotree'
    return 1

  elseif buf_type ==# 'tagbar'
    return 1

  elseif buf_type ==# 'nerdtree'
    return 1

  elseif index(term_buffers, a:buf_nr) >= 0
    " echom 'Console'
    return 1

  else
    return 0

  endif
endfunction

function! LeaveSideBar()
  " Go to a non side bar window
  let loop = 0
  while 1
    let loop = loop + 1
    let bufnr = bufnr('%')

    if loop > 10
      " Don't search for more than 10 windows
      " To handle the case all the windows are 'side bars'
      break
    endif

    if IsSideBar(bufnr)
      wincmd w
    else
      break
    endif

  endwhile
endfunction

command! LeaveSideBar call LeaveSideBar()

function! NERDTreeExplore(...)
  if a:0 == 0
    NERDTree %:p:h
  else
    if isdirectory(a:1)
      execute printf("NERDTree %s", a:1)
    else
      call LeaveSideBar()
      execute printf("e %s", a:1)
    endif
  endif
endfunction

command! -nargs=* -complete=file E call NERDTreeExplore(<f-args>)

I have created a LeaveSideBar command that moves the cursor outside of the side bars (NERDTree but also UndoTree, TagBar, QuickFix, Terminal, ...) and I have replaced the method that open buffer (:e, :bn) by custom variant (:E, :Bn) that call LeaveSideBar first.

Your Answer

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

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