Skip to content

Instantly share code, notes, and snippets.

@pschyska
Created November 23, 2012 17:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pschyska/4136592 to your computer and use it in GitHub Desktop.
Save pschyska/4136592 to your computer and use it in GitHub Desktop.
" Highlight currently open file in nerdtree
" see http://superuser.com/a/474298/175466
" see http://superuser.com/a/509721/175466
" returns true iff is NERDTree open/active
function! rc:isNTOpen()
return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction
" returns true iff focused window is NERDTree window
function! rc:isNTFocused()
return -1 != match(expand('%'), 'NERD_Tree')
endfunction
" returns true iff focused window contains a file in current pwd
function! rc:isInsideCwd()
return -1 != stridx(expand('%:p'), getcwd())
endfunction
" calls NERDTreeFind iff NERDTree is active, current window contains a modifiable file, and we're not in vimdiff
function! rc:syncTree()
if &modifiable && rc:isNTOpen() && !rc:isNTFocused() && strlen(expand('%')) > 0 && !&diff && rc:isInsideCwd()
NERDTreeFind
wincmd p
endif
endfunction
autocmd BufEnter * call rc:syncTree()
@luong-komorebi
Copy link

Exactly what i am looking for, thank you

@Amarlanda
Copy link

which directory do you put this file in?

doesnt work when i put ut in
/Users//.local/share/nvim

@pschyska
Copy link
Author

@Amarlanda This snippet can go into your normal vim config. For neovim, this is ~/.config/nvim/init.vim by default. Just paste the file contents at the end of init.vim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment