Two basic questions with NERDTree — switching windows and finding files - vim

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.

Related

How can I switch windows in Vim

I am using Vim and I am unable to switch to one of my windows by using the default bind keys <ctrl+w> j. I have three windows open and the bottom right window is inaccessible. Why is this happening?
<ctrl> + w + w works for me.
If you :sp or :vsp to split a pane, <ctrl> + w + w will allow you to navigate between them.
It appears as if you have taken something similar to the following course of action
vim <some-file>
:vs <some-other-file>
<C-w> l // to get to the right window
:term // to open up a terminal session within right right window
<C-w> j // to move to the bottom right window (a normal vim window)
:q
vim <file> // within the terminal inside the right vim window
:sp <file> // split that window
Now it will appear as if you have three vim windows, when in reality, you have four:
Two outer (the left, and the right terminal session)
Two within the right window's terminal session
This is quite a precarious position because whether you are in the outer left or right session, the outer buffer (not sure if this is the correct word so please correct me if I'm wrong) will always captures the <C-w> control character for some reason.
You can see this by looking where the <C-w> shows up on the screen when you press it. If I have replicated your environment correctly, it shows up in the bottom right corner below the outer buffer's right window.
As a workaround to this, instead of using <C-w> to proc window navigation, you need to use:
:winc j
to navigate to the bottom right window.

How can I temporarily make the window I'm working on to be fullscreen in vim?

I use vim, and usually have more than one vertical/horizental window open, usually editing c++ header files alongside cpp files. How can I temporarily make the window I'm working on to be fullscreen, edit what I want, and then exit fullscreen?
By fullscreen I mean to fit vim window only, and not my total display screen.
Ctrl+W_ will maximize a window vertically.
Ctrl+W| will maximize a window horizontally.
So far as I'm aware, there is no way to restore the previous layout after these actions, but Ctrl+W= will resize all windows to equal sizes.
An option could be to pursue the editing in a new tab. The following command opens the active buffer into a new tab allowing you to see the buffer in the hole vim window.
:tab split
And close the tab when you're done:
:tabc
Edit:
You can always use the following command to use tt as a shortcut (or better add it to your .vimrc):
:noremap tt :tab split<CR>
and close is when you're done :
:wq
If I understand what you're asking, I think you'll find the ZoomWin plugin helpful (GitHub). If you've got a bunch of split windows, and you want to temporarily make the current window the only visible one, you can hit <C-w>o. When you want to revert to the previous split state, hit <C-w>o again.
[Edit] Note on key mappings:
The default key mapping for this plugin is <C-w>o, but that conflicts with a default Vim key mapping. By default, that does :only, which makes the current window the only window. If you'd like to retain that functionality, you can remap ZoomWin to another key. I remap it to <C-w>w, because I like to use the :only option as well. Here's my mapping:
nnoremap <silent> <C-w>w :ZoomWin<CR>
Note that this also overrides a default Vim mapping, related to moving to other visible windows (:help CTRL-W_w), but I never used that one anyway.
Use Ctrl w_ to maximize the current window vertically.
These are some useful commands that help work with windows:
:e filename - edit another file
:split filename - split window and load another file
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w - move cursor to another window (cycle)
ctrl-w= - make all equal size
10 ctrl-w+ - increase window size by 10 lines
:vsplit file - vertical split
:sview file - same as split, but readonly
:hide - close current window
:only - keep only this window open
:ls - show current buffers
:b 2 - open buffer #2 in this window
I've tried ZoomWin and a few others. The problem is, they all destroy and try to re-create the windows. This is especially problematic with custom plugins like NERDTree, Tagbar and a few others. Icons and fonts are not drawn properly, sizes are messed up etc..
zoomwintab.vim is a simple zoom window plugin that uses vim's tabs feature to zoom into a window inspired by ZoomWin plugin but in a non-destructive manner.
https://github.com/troydm/zoomwintab.vim
I use Tmux, so I mapped it to <leader> z to stay in sync with tmux's <prefix> z
nnoremap <leader>z :ZoomWinTabToggle<CR>
An awesome plugin for toggling windows fullscreen is vim-maximizer.
After it's installed you can simply use <F3> (default shortcut) to toggle fullscreen on the window.
You can also customize the shortcut keys, for example if you wanted to use <C-w> z (similar to tmux shortcut):
nnoremap <silent><C-w>z :MaximizerToggle<CR>
vnoremap <silent><C-w>z :MaximizerToggle<CR>gv
inoremap <silent><C-w>z <C-o>:MaximizerToggle<CR>
Somehow the ZoomWin plugin did not work at all for me, my experience was kind of what arithran says. I couldn't find other plugins so I wrote this:
function! ToggleZoom(zoom)
if exists("t:restore_zoom") && (a:zoom == v:true || t:restore_zoom.win != winnr())
exec t:restore_zoom.cmd
unlet t:restore_zoom
elseif a:zoom
let t:restore_zoom = { 'win': winnr(), 'cmd': winrestcmd() }
exec "normal \<C-W>\|\<C-W>_"
endif
endfunction
augroup restorezoom
au WinEnter * silent! :call ToggleZoom(v:false)
augroup END
nnoremap <silent> <Leader>+ :call ToggleZoom(v:true)<CR>
It creates the effect. You use the mapped key (Leader and + in my case) to toggle between maximized / previous layout. If you change to another split in the same tab, maximization turns off.
Manoj somewhat answered with a lot more useful info, but as a first step this is what works for me:
:hide: Hide the current buffer - with a two-buffer split, this makes the other buffer full screen. Unlike :only, this command does not close unmodified buffers so you can unhide them.
:unhide: Re-create splits for each open buffer
The unhide command does not restore the previous layout, so you will have to manually rearrange your windows if needed ex. using one of these for simple vertical/horizontal splits (this is CTRL-W followed by the uppercase navigation letter - release CTRL-W before entering the letter):
CTRL-W SHIFT-H: Move window to the far left
CTRL-W SHIFT-J: Move window to the very bottom
CTRL-W SHIFT-K: Move window to the very top
CTRL-W SHIFT-L: Move window to the far right
These are the same keys used for navigation, without SHIFT (lowercase navigation letters):
CTRL-W H: Move focus to the left window
CTRL-W J: Move focus to the bottom window
CTRL-W K: Move focus to the top window
CTRL-W L: Move focus to the right window
The H, J, K and K keys alone move the cursor - arrow keys on the keyboard may work as well for navigation.

Open Nerdtree file in non-adjacent split

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>

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.

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