Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created November 11, 2011 18:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRadev/1358783 to your computer and use it in GitHub Desktop.
Save AndrewRadev/1358783 to your computer and use it in GitHub Desktop.
When :edit-ing in NERDTree, open the file in the closest buffer instead
" Whenever you execute an `:edit somefile` command with the cursor in the
" NERDTree, the file gets edited in the NERDTree buffer, which is usually not
" something you want. This autocommand attempts to edit the given file in the
" closest non-NERDTree buffer instead.
"
" It's fairly hacky, but seems to work. I'll probably use it myself, so I'll
" try to remember to update it if I find any problems.
augroup AvoidEditingNERDTree
autocmd!
autocmd BufReadPost * silent call s:BufReadPost()
augroup END
function! s:BufReadPost()
let old_buffer = expand('#')
let new_buffer = expand('%')
if old_buffer =~ 'NERD_tree_\d\+' && len(tabpagebuflist()) > 1
" go to the next window
wincmd w
" edit the given buffer there
exe 'edit '.new_buffer
" go back to the buffer that was the NERDTree
wincmd W
" close what's there now
quit
" open up the NERDTree again
NERDTreeToggle
" go back to the newly edited buffer
wincmd w
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment