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
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. – Perceivableautocmd
, or you can at least close it with:pclose
without the tedium of switching between windows. – Mcnully