NerdTree-显示树中的文件


Answers:


186

在: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>

键盘映射有效,但是如何在vim中调用NERDTreeFind?
azatar 2012年

9
@toszter just:NERDTreeFind
Thomas

1
每当在该选项卡中创建NERDTree时,是否可以将其设置为执行此操作?
2013年

1
@MrA您只能使用NERDTreeFind命令创建NERDTree-够了吗?
托马斯

21

检查一下,它会自动执行同步操作,每当您更改缓冲区时,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()

谢谢,我已经找了这么久了!:)
Gnagno

这个配置在大多数情况下都能很好地工作,但是当我使用coc.nvim转到引用功能时,它弄乱了一切。使用BufRead事件代替已BufEnter解决的问题。
Eddie Cooro

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>

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>

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>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.