NerdTree - 在树中显示文件

时间:2011-10-07 19:52:31

标签: vim nerdtree

是否有快捷方式显示NerdTree目录面板中的当前文件。

像TextMate'在抽屉中显示文件' - Ctrl + Command + R

5 个答案:

答案 0 :(得分:166)

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.

我认为默认情况下它不受任何限制,所以你必须自己做一个键盘绑定。

nmap ,n :NERDTreeFind<CR>

是我的.vimrc中出现的内容,以及

nmap ,m :NERDTreeToggle<CR>

答案 1 :(得分:17)

检查一下,它会自动执行同步操作,每当你更改缓冲区时,nerdtree会自动刷新(我从here复制了一些微小的修改)

" 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()

答案 2 :(得分:6)

这也可能只是一条评论。在当前版本中,切换NerdTree并使用SyncTree导致NERDTree被调用两次。这种修改似乎可以解决该问题:

" 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>

答案 3 :(得分:0)

要配合Chen Rushan的帖子,autocmd BufEnter *调用SyncTree()不会让NERDTree关闭。我找不到能在允许NERDTree切换的同时突出显示NERDTree中当前打开缓冲区的解决方案(以下除外)。

下面是我拼凑的内容,以便能够在使用Ctrl +]进行下一个缓冲区映射时切换NERDTree并突出显示文件。

希望其他人可以改善这一点。

"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>

答案 4 :(得分:0)

陈如山的回答+该评论对我来说非常奏效,除非激活了树。当树打开时,此设置将在树中显示当前文件。

" 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>