NerdTree - Reveal file in tree
Asked Answered
V

6

125

Is there a shortcut which reveal the current file in the NerdTree directory panel.

Like TextMate 'Reveal file in Drawer' - Ctrl+Command+R

Vehicular answered 7/10, 2011 at 19:52 Comment(0)
A
225

in :h NERDTree:

:NERDTreeFind                                                  :NERDTreeFind
    Find the current file in the tree. If no tree exists for the current tab,
    or the file is not under the current root, then initialize a new tree where
    the root is the directory of the current file.

I don't think it's bound to anything by default, so you have to do a keybind yourself.

nmap ,n :NERDTreeFind<CR>

is what appears in my .vimrc, along with

nmap ,m :NERDTreeToggle<CR>
Amata answered 7/10, 2011 at 20:1 Comment(5)
The keymapping works, but how to invoke NERDTreeFind within vim?Impedance
@toszter just :NERDTreeFindAmata
Is there a way to set it to do this whenever the NERDTree is created within that tab?Kraemer
@MrA you can just only create the NERDTree with the NERDTreeFind command - is that enough?Amata
Is there a way to merge :NERDTreeToggleVCS and :NERDTreeFind ? I want reveal current file in the tree with parent being VCS parentAntisyphilitic
S
23

Check this out, it automates the sync operation, whenever you change buffer, the nerdtree will automatically refreshed itself (I copied from here with tiny modifications)

" Check if NERDTree is open or active
function! 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! SyncTree()
  if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufEnter * call SyncTree()
Shirleenshirlene answered 10/2, 2017 at 8:42 Comment(2)
This config works well in most of the cases but it messed up everything when I used coc.nvim go to references functionality. using BufRead event in place of the BufEnter fixed the issue.Syllogize
This opens a second NERDTree buffer when toggling it using :NERDTreeToggle.Lentha
C
9

This should also probably be just a comment. With the current version toggling NerdTree and using SyncTree causes NERDTree to be invoked twice. This modification seems to fix that problem:

" Check if NERDTree is open or active
function! 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! SyncTree()
  if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufEnter * call SyncTree()

function! ToggleNerdTree()
  set eventignore=BufEnter
  NERDTreeToggle
  set eventignore=
endfunction
nmap <C-n> :call ToggleNerdTree()<CR>
Crake answered 29/1, 2020 at 23:21 Comment(0)
H
4

Chen Rushan's answer + the comment worked perfectly well for me, except when the tree is open. With this setting, the current file in the tree will be revealed when the tree is opened.

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

function! CheckIfCurrentBufferIsFile()
  return strlen(expand('%')) > 0
endfunction

" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable
" file, and we're not in vimdiff
function! SyncTree()
  if &modifiable && IsNERDTreeOpen() && CheckIfCurrentBufferIsFile() && !&diff
    NERDTreeFind
    wincmd p
  endif
endfunction

" Highlight currently open buffer in NERDTree
autocmd BufRead * call SyncTree()

function! ToggleTree()
  if CheckIfCurrentBufferIsFile()
    if IsNERDTreeOpen()
      NERDTreeClose
    else
      NERDTreeFind
    endif
  else
    NERDTree
  endif
endfunction

" open NERDTree with ctrl + n
nmap <C-n> :call ToggleTree()<CR>
Hyoscyamine answered 2/7, 2020 at 23:18 Comment(1)
Great!, I would like this script to have another feature also, if it s possible to collapse the other opened trees and open only tree for the current file, it would be more cleaner.Hanan
N
1
function! NerdTreeToggleFind()
    if exists("g:NERDTree") && g:NERDTree.IsOpen()
        NERDTreeClose
    elseif filereadable(expand('%'))
        NERDTreeFind
    else
        NERDTree
    endif
endfunction

nnoremap <C-n> :call NerdTreeToggleFind()<CR>
Nahshu answered 9/4, 2022 at 21:38 Comment(0)
S
0

To go along with Chen Rushan's post, the

autocmd BufEnter * call SyncTree()

won't let NERDTree close. I couldn't find a solution (other than below) that would highlight the current open buffer in NERDTree while allowing NERDTree to Toggle.

Below is what I scraped together to be able to toggle NERDTree and have files highlighted while using Ctrl + ] for my next buffer mapping.

Hopefully others can improve this.

"Buffers
set hidden

function! IsNERDTreeOpen()        
  return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction

function! NextBuffer()
     bnext
  if IsNERDTreeOpen() 
       NERDTreeFind
       wincmd p
  endif
endfunction

nnoremap <c-]> <Esc>:call NextBuffer()<CR>

function! PrevBuffer()
     bprev
  if IsNERDTreeOpen() 
       NERDTreeFind
       wincmd p
  endif
endfunction

nnoremap <c-[> <Esc>:call PrevBuffer()<CR>

function! ToggleNT()
    NERDTreeToggle
endfunction

map <c-u> <Esc>:call ToggleNT()<cr>
Sublett answered 5/12, 2019 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.