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.)

  • The whole point of NERDTree is precisely to not work like netrw. If you want netrw use netrw.
    – romainl
    Oct 7, 2014 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, 2014 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, 2014 at 6:45

1 Answer 1


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: 


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:

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

  • 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, 2016 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .