Open Nerdtree file in non-adjacent split - vim

Normally I switch over to the Nerdtree file buffer by hitting Ctrl+H twice to move the cursor over to the left edge of the screen. However, this means that when I open files, they are always in the split adjacent to the NERDTree window. How can I open files in the split that's not adjacent to the NERDTree window?

NERDtree will open a file in the previous window if you press o or enter. You can open it in a split of the previous window by pressing i, or in a vertical split by pressing s.
It sounds like you have Ctrl-H mapped to Ctrl-W h. The trick to not having NERDtree open the file into a window that is adjacent to the NERDTree window is to not have the adjacent window be the previous window.
To achieve this, toggle the NERDTree window closed. (:NERDTreeToggle, or whatever your mapkey is to toggle NERDTree). Then move to the appropriate window you want to open the file in. Then toggle NERDTree open again. Now you can open the file in the previous window that you were in using o or enter... or in splits using i or s.
To save having to toggle the NERDTree window open/close, you may want to set the option NERDTreeQuitOnOpen. Then you just need to open NERDTree when you're in a window that you intend to open a file into.

I usually use :NERDTreeFind in order to move from the right split window to the NERDtree on the very left.
This command shows the currently open file within the NERDtree window. Based on this, the previous window is the one on the right side. This of course only works conveniently if the file you want to open is close in the directory tree to the file already open.
To quickly access :NERDTreeFind, I mapped it to Ctrl + m in my ~/.vimrc:
nnoremap <C-m> :NERDTreeFind<CR>

Related

gvim: file list on split window, open in top screen

I have a split window open in GVIM.
On the bottom window I have a text file open.
This text file contains a list of files (with full path).
What I would like is a command so that according to my cursor position on a file name in the bottom window, that file will open on the top window. At the moment, when I press g-f it opens it in the bottom window instead of the file list.
Any ideas?
EDIT: I managed to solve it with a macro (copy line, ctrl-w, up, :e and paste). Any better suggestions are welcome!
You could use a mapping like this one:
nnoremap <F7> :let cf = expand("<cfile>") <bar> wincmd k <bar> execute "e " . cf<CR>
save the filename under the cursor in a variable,
move the cursor to the window above,
edit the file.
But what problem are you trying to solve?

NERDTree: when you have a split, pressing enter will open the file in which split?

So I am confused by what's the proper behavior of NERDTree
When I go to NERDTree and press 's', a split happens on the RIGHT side of the screen (I have splitright enabled).
Now if I go back to the NERDTree split and press enter on another file, the LEFT split gets replaced.
I would like all opening of files to happen on the right split. Is there a way to do this?

VIM: How can i open a file at right side as vsplit from a left side NerdTree panel?

I installed NerdTree plugin.
How can i open a file at right side as vsplit from a left side NerdTree panel ?
To make vsplit put the new buffer on the right of the current buffer:
set splitright
Similarly, to make split put the new buffer below the current buffer:
set splitbelow
I haven't tried this with NerdTree, however.
There's a s command, but it opens a file split to the left of current buffer. Though you can press Ctrl+W r to swap windows then.
This is a bit of a hack, but how I do it is this:
Put cursor in window I want to open file into
Hit <leader>n<leader>n (this closes NERDtree and then opens it again with the cursor in NERDtree)
Select the file
On my system this opens it on the last window I was just on if the file isn't already open on my screen.
Start in the window you want to open your file into.
Hit <leader>n<leader>n to close and reopen NERDTree
Select the file you want!
A quick C-w = will get your windows back to proper proportions.
Not sure if anyone else is still struggling with this, but here's how I dealt with it.
You can use
:ls
to list the available buffers. Which would look something like:
1 "foo.txt"
2 "blame_the_user.java"
:b1 to select foo.txt
:b2 for blame_the_user.java
This method can be done from any window setup using :sp or :vs.
Open the buffers you want to split first.

Two basic questions with NERDTree — switching windows and finding files

I am using vim with NERDTree to develop, but not good at it. I have two questions with NERDTree:
How to move cursor between nav window and edit window without mouse?
How to go to one special file node (named xxx) in nav window directly by keyboard?
The NERDTree plugin creates a Vim buffer, and displays it in a Vim window. So you can use any of your normal Vim commands.
(1) Thus, if your edit window is to the right of the NERDTree window, you would use: ctrl + Wl to go to right window and ctrl + Wh to go to left window. or ctrl + w twice to toggle between the two.
(2) To go to a special file, simply search for it while in the NERDTree window:
/xxx
Hit ENTER to end the search at the line, and then ENTER again to open the file in the previous window and go to it.

How to jump back to NERDTree from file in tab?

I usually:
Choose the needed file.
Open it in a tab(t character, by default).
But how I can jump back to NERDTree to open one more file in a tab?
Temporary solution I use now in my .vimrc file:
map <F10> :NERDTree /path/to/root/of/my/project
But it's not very useful to start navigation again and again from the root directory.
Ctrl-ww
This will move between open windows (so you could hop between the NERDTree window, the file you are editing and the help window, for example... just hold down Ctrl and press w twice).
Ctrl+ww cycle though all windows
Ctrl+wh takes you left a window
Ctrl+wj takes you down a window
Ctrl+wk takes you up a window
Ctrl+wl takes you right a window
NERDTree opens up in another window. That split view you're seeing? They're called windows in vim parlance. All the window commands start with CTRL-W. To move from adjacent windows that are left and right of one another, you can change focus to the window to the left of your current window with CTRL-w h, and move focus to the right with CTRL-w l. Likewise, CTRL-w j and CTRL-w k will move you between horizontally split windows (i.e., one window is above the other). There's a lot more you can do with windows as described here.
You can also use the :NERDTreeToggle command to make your tree open and close. I usually bind that do t.
If you use T instead of t there is no need to jump back because the new tab will be opened, but vim's focus will simply remain within NERDTree.
You can focus on a split window using # ctrl-ww.
for example, pressing:
1 ctrl-ww
would focus on the first window, usually being NERDTree.
Since it's not mentioned and it's really helpful:
ctrl-wp
which I memorize as go to the previously selected window.
It works as a there and back command. After having opened a new file from the tree in a new window press ctrl-wp to switch back to the NERDTree and use it again to return to your previous window.
PS: it is worth to mention that ctrl-wp is actually documented as go to the preview window (see: :help preview-window and :help ctrl-w).
It is also the only keystroke which works to switch inside and explore the COC preview documentation window.
ctrl-ww Could be useful when you have limited tabs open. But could get annoying when you have too many tabs open.
I type in :NERDTree again to get the focus back on NERDTree tab instantly wherever my cursor's focus is. Hope that helps
The top answers here mention using T to open a file in a new tab silently, or Ctrl+WW to hop back to nerd-tree window after file is opened normally.
IF WORKING WITH BUFFERS: use go to open a file in a new buffer, silently, meaning your focus will remain on nerd-tree.
Use this to open multiple files fast :)
You can change the tabs by ctrl-pgup and ctrl-pgdown. On that tab you came from the NERDTree is still selected and you can open another tab.
In more recent versions of NERDTree you can use the command :NERDTreeFocus, which will move focus to the NERDTree window.
gt = next Tap
gT = previous Tab
if you want you can enable the mouse support editing ~/.vimrc file.
put set mouse=a
after that you enable files click in NERDTree.
All The Shortcuts And Functionality is At
press CTRL-?

Resources