13

我可以遍历 NERDTree,但是要查看文件内容,我按go,一旦打开文件的缓冲区就会保持打开状态,直到我明确关闭它。这使得查看文件太不舒服了。

当我遍历 NERDTree 节点时,我想在临时查看器缓冲区中查看突出显示的文件内容,并且我想明确地选择一些遍历的文件进行编辑,比如按e

当我关闭 NERDTree 缓冲区时,临时查看器缓冲区也将关闭,并且应该只为那些明确选择的文件打开缓冲区,而不是所有查看的文件。

那可能吗?

4

3 回答 3

5

看起来这可能是对 NERDTree 的一个很好的功能请求:)

同时,你可以在你的 ~/.vimrc 中加入如下内容:

let g:nerd_preview_enabled = 0
let g:preview_last_buffer  = 0

function! NerdTreePreview()
  " Only on nerdtree window
  if (&ft ==# 'nerdtree')
    " Get filename
    let l:filename = substitute(getline("."), "^\\s\\+\\|\\s\\+$","","g")

    " Preview if it is not a folder
    let l:lastchar = strpart(l:filename, strlen(l:filename) - 1, 1)
    if (l:lastchar != "/" && strpart(l:filename, 0 ,2) != "..")

      let l:store_buffer_to_close = 1
      if (bufnr(l:filename) > 0)
        " Don't close if the buffer is already open
        let l:store_buffer_to_close = 0
      endif

      " Do preview
      execute "normal go"

      " Close previews buffer
      if (g:preview_last_buffer > 0)
        execute "bwipeout " . g:preview_last_buffer
        let g:preview_last_buffer = 0
      endif

      " Set last buffer to close it later
      if (l:store_buffer_to_close)
        let g:preview_last_buffer = bufnr(l:filename)
      endif
    endif
  elseif (g:preview_last_buffer > 0)
    " Close last previewed buffer
    let g:preview_last_buffer = 0
  endif
endfunction

function! NerdPreviewToggle()
  if (g:nerd_preview_enabled)
    let g:nerd_preview_enabled = 0
    augroup nerdpreview
      autocmd!
      augroup END
  else
    let g:nerd_preview_enabled = 1
    augroup nerdpreview
      autocmd!
      autocmd CursorMoved * nested call NerdTreePreview()
    augroup END
  endif
endfunction

这可能是非常幼稚和讨厌的代码,但是通过一些调整可以完成您打算做的事情。

已编辑,版本 2 中的更改:

  • 在自动命令上添加了嵌套,因此语法高亮有效
  • 默认不开启,执行 :call NerdPreviewToggle() 开启/关闭
于 2014-03-04T21:31:08.017 回答
0

我通过考虑制表符来构建 DavidEG 的答案,因为我遇到了多个 nerdtree 制表符边缘情况。

function! PreviewNERDTreeFile()
  if !exists('t:previous_preview_buffer') | let t:previous_preview_buffer = 0 | endif
  let filename = substitute(getline('.'), '^\s*\|\s*$', '','g')
  let should_close_buffer_next_time = 1
  if (bufnr(filename) > 0) | let should_close_buffer_next_time = 0 | endif

  normal go
  if t:previous_preview_buffer > 0
    exe 'bwipeout ' . t:previous_preview_buffer
    let t:previous_preview_buffer = 0
  endif
  if should_close_buffer_next_time
    let t:previous_preview_buffer = bufnr(filename)
  endif
endfunction
于 2018-02-13T04:36:04.130 回答
0

Here's a NERDTree extension that uses the last active buffer as a preview window, and either hijacks that window or splits it with the original content depending on if you open with o, s, i, gs, gi etc.

https://github.com/numEricL/nerdtree-live-preview

于 2021-02-06T05:27:50.997 回答