Is it possible to just view files content when traverse NERDTree?
Asked Answered
A

3

14

I can traverse NERDTree, but to see file content I press go, and once opened file's buffer stays open until I explicitely close it. That makes viewing files too uncomfortable.

when I traverse NERDTree nodes I'd like to see the highlighted file content in a temporary viewer buffer and I'd like to explicitely select some of traversed files for editing, say by pressing e.

When I close NERDTree buffer, the temporary viewer buffer shall close as well, and there should only be opened buffers for those explicitely selected files, not for all viewed files.

Is that possible?

Apennines answered 29/10, 2012 at 10:25 Comment(2)
netrw has a "Preview" mapping (p) but it's not automatic and the preview window is not closed automatically. NERDTree doesn't do any of that: you'll have to write your own functions for all of that.Perceivable
What you can do is cause it to be closed automatically with an autocmd, or you can at least close it with :pclose without the tedium of switching between windows.Mcnully
V
5

Looks like that could be a nice feature request for NERDTree :)

Meanwhile, you could put in your ~/.vimrc something like the following:

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

This is probably quite naive and nasty code, but with some tweaking could do what you intend to do.

Edited, changes in version 2:

  • Added nested on the autocommand so syntax highlight works
  • Not enabled by default, execute :call NerdPreviewToggle() to enable/disable
Virchow answered 29/10, 2012 at 10:25 Comment(5)
That is cool!!! But preview buffer lost syntax highlighting. How can I enable syntax highlight for preview buffer?Apennines
Good point, just edited the answer to add the nested keyword that does it work properly (:help autocmd-nested)Virchow
Hmmm, yes, but looks like only previously opened files (existing buffers) are highlighted.Apennines
Just check it and seems to work in all the files :-\ :-/ Are you sure?Virchow
Also if I stay at the folder node and the second node in tree is the first file in that folder, then when I move cursor back and forth to the file and to the folder (if the file was not open explicitely the file) buffer closes every second time I put cursor on it. In case the file was explicitely open then it is highlighted and the buffer doesn't close every second time cursor puts on the file node, which is the normal behaviorApennines
A
0

I built on DavidEG's answer by taking tabs into account, because I ran into multiple nerdtree tab edge-cases.

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
Already answered 13/2, 2018 at 4:36 Comment(0)
S
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

Safranine answered 6/2, 2021 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.