是否可以在遍历NERDTree时查看文件内容?

时间:2012-10-29 10:25:16

标签: vim nerdtree

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

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

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

这可能吗?

3 个答案:

答案 0 :(得分: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中的更改:

  • 添加嵌套在自动命令上,因此语法高亮显示工作
  • 默认情况下未启用,执行:调用NerdPreviewToggle()以启用/禁用

答案 1 :(得分: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

答案 2 :(得分:0)

这是一个 NERDTree 扩展,它使用最后一个活动缓冲区作为预览窗口,并根据您是否使用 o, s, i, gs, gi 等打开来劫持该窗口或将其与原始内容分开。

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