vim NerdTree -在树中显示文件

zvms9eto  于 2022-11-11  发布在  其他
关注(0)|答案(6)|浏览(246)

是否有快捷方式显示当前文件在NerdTree目录面板。
类似于TextMate '在抽屉中显示文件' - Ctrl+Command+R

q7solyqu

q7solyqu1#

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

ddarikpa2#

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

vlf7wbxs3#

这也可能只是一个注解。在当前版本中,切换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>
fxnxkyjh

fxnxkyjh4#

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

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

owfi6suc5#

要沿着陈如山的帖子,那

autocmd BufEnter * call SyncTree()

我不会让NERDTree关闭。我找不到一个解决方案(除了下面的),可以在允许NERDTree切换的同时突出显示当前打开的缓冲区。
下面是我拼凑起来的,以便在使用Ctrl + ]进行下一个缓冲区Map时能够切换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>
k5ifujac

k5ifujac6#

function! NerdTreeToggleFind()
    if exists("g:NERDTree") && g:NERDTree.IsOpen()
        NERDTreeClose
    elseif filereadable(expand('%'))
        NERDTreeFind
    else
        NERDTree
    endif
endfunction

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

相关问题