Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

nerdtree

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?

like image 668
zuba Avatar asked Oct 29 '12 10:10

zuba


People also ask

How do I show hidden files in NERDTree?

Press I ( Shift + i ) to toggle hidden files in the NERDTree explorer window. For more detail, access the NERDTree help file :help NERD_tree. txt and search for "hidden". And to hide hidden files by default, put let NERDTreeShowHidden=0 into your vimrc.

How do I open a NERDTree file?

If you want to use NERDTree, you can open NERDTree (type :NERDTree ), navigate to your file, and then press t with cursor on the file name. This will open specific file in new tab.

How do I open a NERDTree file in a new tab?

7 Answers. Show activity on this post. s will open the file currently under the cursor in a new vertically split window. Use t to open in a new tab.


1 Answers

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
like image 180
2 revs Avatar answered Sep 26 '22 09:09

2 revs