NerdTree-在树中显示文件

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

就像 TextMate“在抽屉里显示文件”-Ctrl + Command + R

34305 次浏览

书呆子树:

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

看看这个,它自动执行同步操作,每当你改变缓冲区,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()

配合陈如山的帖子,

autocmd BufEnter * call SyncTree()

不让 NERDTree 关门。我无法找到一个解决方案(除了下面) ,突出显示 NERDTree 中当前打开的缓冲区,同时允许 NERDTree 切换。

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

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

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

这也可能只是一个评论。使用当前版本切换 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>

陈如山的回答 + 评论对我来说非常好用,除了树开着的时候。有了这个设置,树中的当前文件将在树打开时显示出来。

" 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>
function! NerdTreeToggleFind()
if exists("g:NERDTree") && g:NERDTree.IsOpen()
NERDTreeClose
elseif filereadable(expand('%'))
NERDTreeFind
else
NERDTree
endif
endfunction


nnoremap <C-n> :call NerdTreeToggleFind()<CR>