NERDTree live-preview (like sublime sidebar)
Asked Answered
G

3

6

Sublime's sidebar has a cool feature, where I can just press the arrow keys and get a quick glance of what each file looks like in the editor pane. It doesn't actually open the file -- just shows it in the editor pane.

I want to do the same thing with NERDTree in Vim (or Vinegar/netrw, doesn't really matter). I know NERDTree lets me use go to open the file under the cursor while keeping the tree in focus, but (a) that requires two keystrokes, and (b) it creates a new buffer for every file I "preview" like this, so... not much of a preview really.

Is there a way to make NERDTree or Vim emulate this Sublime feature?

Goose answered 8/2, 2015 at 16:18 Comment(0)
T
13

Yes, there is. Vim has a feature called "preview window". You can open a file in the preview window with :pedit <filename>. If you want to plug this into NERDTree, you could create a file in the ~/.vim/nerdtree_plugin/ directory, for example "live_preview_mapping.vim", with the following contents:

if exists("g:loaded_nerdree_live_preview_mapping")
  finish
endif
let g:loaded_nerdree_live_preview_mapping = 1

call NERDTreeAddKeyMap({
      \ 'key':           '<up>',
      \ 'callback':      'NERDTreeLivePreview',
      \ 'quickhelpText': 'preview',
      \ })

function! NERDTreeLivePreview()
  " Get the path of the item under the cursor if possible:
  let current_file = g:NERDTreeFileNode.GetSelected()

  if current_file == {}
    return
  else
    exe 'pedit '.current_file.path.str()
  endif
endfunction

The first part is simply a load guard, so the file is sourced only once, just boilerplate. The second part adds a keymap using the NERDTree API for the <up> key that calls the given callback function.

The callback function is the meat of the code, but it should be fairly easy to understand -- it takes the node under the cursor, if there is one, and executes a :pedit with the filename.

You can even do this more easily with a simple filetype-specific mapping, something like this:

autocmd FileType nerdtree nnoremap <buffer> <up> :call NERDTreeLivePreview()<cr>

But the former is the approach recommended by the plugin (see :help NERDTreeAPI). If nothing else, this adds a help entry to the ? key for it, and it keeps nerdtree extensions in one place.

For more info on what you can do with the preview window, try :help preview-window. For instance, you can close it with <c-w>z, but you can map that to whatever you'd like, that's not really related to the NERDTree anymore. If you're unhappy with where the window shows up, consider changing the "pedit" to "botright pedit" or "leftabove pedit" or whatever you want. Check the help for :leftabove and take a look at the related commands below.

Tojo answered 9/2, 2015 at 10:45 Comment(2)
Excellent answer. The only requirement it doesn't address is not opening a new buffer for the file. While you probably can't get around creating the buffer, there might be a reasonable way to delete it once the preview window is closed (or switches to a different buffer).Mandimandible
I actually think that OP meant that a new window is opened for every go, and that doesn't really work as a "preview", since it doesn't open the files in a single window. As you say, you can't not create a buffer if you're opening the file. But you're right that the buffer doesn't get cleaned up. It depends on the global hidden option, but it might make sense to get the preview buffer wiped in particular. I guess it would be possible to write a mapping to do that, but wouldn't be super straightforward.Tojo
P
0

NERDTree doesn't offer anything automatic out of the box. I like a preview window that hijacks the last active window and allows for opening the buffer there, or to split with the original buffer. This extension does that, and its source code is pretty short.

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

Pedigo answered 6/2, 2021 at 5:22 Comment(0)
P
-1

With netrw, to preview a file: with the cursor atop a file, press "p".

Philologian answered 20/2, 2015 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.