3

When I open a file in a NERDTree buffer that was created by opening a directory, when I press <CR>, it opens the selected file in the current window unless the file is open in another window, in which case it moves the cursor to that window instead (changing tabs if required).

Is there anyway to have NERDTree open the file in the buffer NERDTree is in, instead?

(I'm essentially wanting to use NERDTree as a netrw replacement. In this aspect, NERDTree's behavior differs from that of netrw; I would like it to mimic netrw here.)

3
  • The whole point of NERDTree is precisely to not work like netrw. If you want netrw use netrw. – romainl Oct 7 '14 at 12:43
  • @romainl: And yet one of it's features is to act as a netrw replacement. I don't want it to work like netrw is every respect: that would defeat the point; only in this one instance. NERDTree does a lot things by default that I greatly prefer, which is why I use it, and not netrw. In this particular case, I'm attempting to simply open another file side-by-side (but it happens to be open in another tab), but perhaps I don't remember the filename. I split the window, did a :e %:h, and found the file. Now I want to open it, here. – Thanatos Oct 8 '14 at 4:04
  • 2
    NERDTree doesn't have the feature you ask for because it is specifically designed to not work that way. It is a side panel, not a generic file browser like netrw. The "NERDTree way" would be to 1. summon NERDTree, 2. open the file under the cursor in a new vsplit with s and have NERDTree close itself automatically with let NERDTreeQuitOnOpen = 1. – romainl Oct 8 '14 at 6:45
4
+50

It seems you are looking for the option NERDTreeHijackNetrw, as explained in the NERDTree documentation:

If set to 1, doing a
:edit <some directory>

will open up a "secondary" NERD tree instead of a netrw in the target window.
Secondary NERD trees behaves slightly different from a regular trees in the
following respects:
1. 'o' will open the selected file in the same window as the tree,
replacing it.
2. you can have as many secondary tree as you want in the same tab.

If you are looking for something beyond that you could achieve it based on the API offered by the plugin:

4. The NERD tree API *NERDTreeAPI*
The NERD tree script allows you to add custom key mappings and menu items via
a set of API calls. Any scripts that use this API should be placed in
~/.vim/nerdtree_plugin/ (*nix) or ~/vimfiles/nerdtree_plugin (windows).

The script exposes some prototype objects that can be used to manipulate the
tree and/or get information from it: 

g:NERDTreePath
g:NERDTreeDirNode
g:NERDTreeFileNode
g:NERDTreeBookmark

See the code/comments in NERD_tree.vim to find how to use these objects. The
following code conventions are used:
* class members start with a capital letter
* instance members start with a lower case letter
* private members start with an underscore

See this blog post for more details:
http://got-ravings.blogspot.com/2008/09/vim-pr0n-prototype-based-objects.html

One possible solution is to create a mapping that retrieve the path from the file under the cursor and use the :edit on it.

1
  • This doesn't actually work for me; NERDTreeHijackNetrw is set to (and defaults to) 1, but pressing Enter or o on a file highlighted in a NERDTree opened by using :edit <a directory> does not cause that file to replace NERDTree in that window. I think the documentation is simply not bothering to mention the case of this particular question; in all other respects, o/Enter works as documented. – Thanatos Oct 5 '16 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.