是否有快捷方式在NerdTree目录面板中显示当前文件。
就像TextMate“抽屉中的显示文件”一样-Ctrl + Command + R
是否有快捷方式在NerdTree目录面板中显示当前文件。
就像TextMate“抽屉中的显示文件”一样-Ctrl + Command + R
Answers:
在: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>
:NERDTreeFind
检查一下,它会自动执行同步操作,每当您更改缓冲区时,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()
BufRead
事件代替已BufEnter
解决的问题。
这也可能只是一个评论。在当前版本中,切换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>
为了配合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>
只有将树激活后,陈汝山的回答和评论对我来说才非常有效。当树打开时,此设置将在树中显示当前文件。
" 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>