Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to highlight current file in NERDTree

📅 2014-May-05 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

I typically navigate in source files using Ctags information in Vim. It would be useful if I could see where in the directory hierarchy the file I jumped into is. I can see this in the NERDTree window using :NERDTreeFind.

However, it would be neat if this happened automatically when I jumped into a file. Here is code from Lambart that makes it happen. Just add it to your vimrc file to take effect:

" Check if NERDTree is open or active
function! rc:isNERDTreeOpen()        
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable
" file, and we're not in vimdiff
function! rc:syncTree()
  if &modifiable && rc:isNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufEnter * call rc:syncTree()

Tried with: Vim 7.4 and Ubuntu 14.04