easier way to navigate between vim split panes - vim

I am using NERDTree on vim and usually open files with i
Is there an easy way to switch between different panes? Currently I use CTRL+W+W to move from one pane to another.

Long ago I found a tip (once on vim.org, now on wikia, apparently) that I've stuck with. Remap ctrl-[hjkl] to navigate splits. It has served me well.
" Use ctrl-[hjkl] to select the active split!
nmap <silent> <c-k> :wincmd k<CR>
nmap <silent> <c-j> :wincmd j<CR>
nmap <silent> <c-h> :wincmd h<CR>
nmap <silent> <c-l> :wincmd l<CR>

I prefer hitting single keys over hitting key-chords. The following maps pane movement to arrow keys:
" Smart way to move between panes
map <up> <C-w><up>
map <down> <C-w><down>
map <left> <C-w><left>
map <right> <C-w><right>

I know this is an old question, but I have a perfect way. Using the number of the split.
split_number C-w C-w
The panes are numbered from top-left to bottom-right with the first one getting the number 1.
for example to go to split number 3 do this 3 C-w C-w, press Ctrl-w twice.

Key mappings are definitely the way to go. I use the mappings mentioned by overthink. I also include the following mappings in my vimrc to move the splits themselves.
" Move the splits arround!
nmap <silent> <c-s-k> <C-W>k
nmap <silent> <c-s-j> <C-W>j
nmap <silent> <c-s-h> <C-W>h
nmap <silent> <c-s-l> <C-W>l
This makes it so that if the split opens in the wrong spot (lets say the left side and I want it on the right) I go to that split and hit <C-S-l> and the split moves where I want it to.

In order to be consistent with changing tabs via gt & gT, I'm currently trying out the g mappings for changing splits. I tend to hit the shift key as I go for the Ctrl key so this helps me avoid that mistake until I get better at not doing so.
nnoremap gh <C-W><C-H>
nnoremap gj <C-W><C-J>
nnoremap gk <C-W><C-K>
nnoremap gl <C-W><C-L>

I have mapped ctrl+w ctrl+w to <tab> (under normal mode as in normal mode tab does not have any use)and that's have made my life easier as now I can switch between panes easily by pressing <tab>.
For switching to a particular pane, I can press <i> + <tab> to switch between panes as split window panes also got their own number which can replace i.
Ex. i = 1,2...n.

Very easy way of achieving it. Type this shortcut twice, and that should work
ctrl+w ctrl+w

Related

How do I map CMD (Command ⌘) to shift MacVim window

I know that I can use ctrl+w to shift between different Vim windows. But how do I remap the CMD-key to replace ctrl+w in various way? I'm specifically looking to bind cmd with the arrow keys, so that I can shift to NERDTree easily with CMD+LeftArrow. Appreciate the assistance.
I've tried to add the following to $MYVIMRC...
nmap <silent> <D-h> :wincmd h<CR> // For going to NERDTree
nmap <silent> <D-l> :wincmd l<CR> // For going back to file I'm working on.
In the left-hand side of a mapping, Command+Left is written <D-Left>. With this and other issues fixed (see below), your mappings should look like this:
nnoremap <D-Left> <Cmd>wincmd h<CR>
nnoremap <D-Right> <Cmd>wincmd l<CR>
or, simply, like this:
nnoremap <D-Left> <C-w>h
nnoremap <D-Right> <C-w>l
Other issues:
Recursive mappings (nmap) should be reserved to the rare situations where you want to use another mapping in your mapping. This is not the case, here, so nnoremap is the best choice.
The mapped commands don't echo anything so <silent> is useless.
Vim's comment leader is ", not //.
You can't have comments on the same line as a mapping anyway, see :help map-comments.
The newish :help <Cmd> is cleaner than using : in the right-hand side of a mapping.
Note that these mappings only work in the MacVim GUI.

How do I navigate buffers in vim?

I normally work with more than 5 files at a time. I use buffers to open different files.
I use commands such as :buf file1, :buf file2 etc.
Is there a faster way to move to different files?
What I use:
Go to the previous buffer: :b# or :bp
Go to the next buffer: :bn
If you know your file is loaded in buffer 5: :b5
To get a list of buffers: :buffers or shorter: :ls
And have a short look on :he buffer
And the wiki entry on Easier Buffer Switching on the Vim Wiki: http://vim.wikia.com/wiki/Easier_buffer_switching
SO already has a question regarding yours: How do you prefer to switch between buffers in Vim?
A few mappings can make your life a lot easier.
This one lists your buffers and prompts you for a number:
nnoremap gb :buffers<CR>:buffer<Space>
This one lists your buffers in the "wildmenu". Depends on the 'wildcharm' option as well as 'wildmenu' and 'wildmode':
nnoremap <leader>b :buffer <C-z>
These ones allow you to cycle between all your buffers without much thinking:
nnoremap <PageUp> :bprevious<CR>
nnoremap <PageDown> :bnext<CR>
Also, don't forget <C-^> which allows you to alternate between two buffers.
Below I describe some excerpts from sections of my .vimrc. It includes mapping the leader key, setting wilds tab completion, and finally my buffer nav key choices (all mostly inspired by folks on the interweb, including romainl). Edit: Then I ramble on about my shortcuts for windows and tabs.
" easier default keys {{{1
let mapleader=','
nnoremap <leader>2 :#"<CR>
The leader key is a prefix key for mostly user-defined key commands (some plugins also use it). The default is \, but many people suggest the easier to reach ,.
The second line there is a command to # execute from the " clipboard, in case you'd like to quickly try out various key bindings (without relying on :so %). (My nmeumonic is that Shift-2 is #.)
" wilds {{{1
set wildmenu wildmode=list:full
set wildcharm=<C-z>
set wildignore+=*~ wildignorecase
For built-in completion, wildmenu is probably the part that shows up yellow on your Vim when using tab completion on command-line. wildmode is set to a comma-separated list, each coming up in turn on each tab completion (that is, my list is simply one element, list:full). list shows rows and columns of candidates. full's meaning includes maintaining existence of the wildmenu. wildcharm is the way to include Tab presses in your macros. The *~ is for my use in :edit and :find commands.
" nav keys {{{1
" windows, buffers and tabs {{{2
" buffers {{{3
nnoremap <leader>bb :b <C-z><S-Tab>
nnoremap <leader>bh :ls!<CR>:b<Space>
nnoremap <leader>bw :ls!<CR>:bw<Space>
nnoremap <leader>bt :TSelectBuffer<CR>
nnoremap <leader>be :BufExplorer<CR>
nnoremap <leader>bs :BufExplorerHorizontalSplit<CR>
nnoremap <leader>bv :BufExplorerVerticalSplit<CR>
nnoremap <leader>3 :e#<CR>
nmap <C-n> :bn<cr>
nmap <C-p> :bp<cr>
The ,3 is for switching between the "two" last buffers (Easier to reach than built-in Ctrl-6). Nmeuonic is Shift- 3 is #, and # is the register symbol for last buffer. (See :marks.)
,bh is to select from hidden buffers (!).
,bw is to bwipeout buffers by number or name. For instance, you can wipeout several while looking at the list, with ,bw 1 3 4 8 10 <CR>. Note that wipeout is more destructive than :bdelete. They have their pros and cons. For instance, :bdelete leaves the buffer in the hidden list, while :bwipeout removes global marks (see :help marks, and the description of uppercase marks).
I haven't settled on these keybindings, I would sort of prefer that my ,bb was simply ,b (simply defining while leaving the others defined makes Vim pause to see if you'll enter more).
Those shortcuts for :BufExplorer are actually the defaults for that plugin, but I have it written out so I can change them if I want to start using ,b without a hang.
You didn't ask for this:
If you still find Vim buffers a little awkward to use, try to combine the functionality with tabs and windows (until you get more comfortable?).
" windows {{{3
" window nav
nnoremap <leader>w <C-w>
nnoremap <M-h> <C-w>h
nnoremap <M-j> <C-w>j
nnoremap <M-k> <C-w>k
nnoremap <M-l> <C-w>l
" resize window
nnoremap <C-h> <C-w><
nnoremap <C-j> <C-w>+
nnoremap <C-k> <C-w>-
nnoremap <C-l> <C-w>>
Notice how nice ,w is for a prefix. Also, I reserve Ctrl key for resizing, because Alt (M-) is hard to realize in all environments, and I don't have a better way to resize. I'm fine using ,w to switch windows.
" tabs {{{3
nnoremap <leader>t :tab
nnoremap <M-n> :tabn<cr>
nnoremap <M-p> :tabp<cr>
nnoremap <C-Tab> :tabn<cr>
nnoremap <C-S-Tab> :tabp<cr>
nnoremap tn :tabe<CR>
nnoremap te :tabe<Space><C-z><S-Tab>
nnoremap tf :tabf<Space>
nnoremap tc :tabc<CR>
nnoremap to :tabo<CR>
nnoremap tm :tabm<CR>
nnoremap ts :tabs<CR>
nnoremap th :tabr<CR>
nnoremap tj :tabn<CR>
nnoremap tk :tabp<CR>
nnoremap tl :tabl<CR>
" or, it may make more sense to use
" nnoremap th :tabp<CR>
" nnoremap tj :tabl<CR>
" nnoremap tk :tabr<CR>
" nnoremap tl :tabn<CR>
In summary of my window and tabs keys, I can navigate both of them with Alt, which is actually pretty easy to reach. In other words:
" (modifier) key choice explanation {{{3
"
" KEYS CTRL ALT
" hjkl resize windows switch windows
" np switch buffer switch tab
"
" (resize windows is hard to do otherwise, so we use ctrl which works across
" more environments. i can use ',w' for windowcmds o.w.. alt is comfortable
" enough for fast and gui nav in tabs and windows. we use np for navs that
" are more linear, hjkl for navs that are more planar.)
"
This way, if the Alt is working, you can actually hold it down while you find your "open" buffer pretty quickly, amongst the tabs and windows.
Once the buffers are already open, you can just type :b partial_filename to switch
So if :ls shows that i have my ~./vimrc open, then I can just type :b vimr or :b rc to switch to that buffer
There are many ways to solve. The best is the best that WORKS for YOU. You have lots of fuzzy match plugins that help you navigate. The 2 things that impress me most are
1) CtrlP or Unite's fuzzy buffer search
2) LustyExplorer and/or LustyJuggler
And the simplest :
:map <F5> :ls<CR>:e #
Pressing F5 lists all buffer, just type number.

vimdiff and move among left and right pane

I am using vimdiff for the first time. Online I found written that to move from the left pane you use CTRL + w + Left or right arrow
This does not work for me. But I see that if I press just CTRL + w and press w for a sec and let it go, it switches pane after ~500ms.
Is this how it is supposed to work? Am I doing something wrong?
Ctrl+w and right and left arrow can be used to move between any split windows on vim, not only vimdiff splits.
These keys do work here on cygwin; also, Ctrl+w w also moves to the next window, but without the delay you mentioned.
It is possible that you have mapped these keys in your .vimrc or via some vim plugin. You can check this with :map w, :map <left> and :map <right>.
As moving between windows is something that you use often, you may consider using the following mappings:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
Then you can use Ctrl+h and Ctrl+l to move left and right, without moving your hands from the home row. And the nnoremap will ensure that these works despite of any other mappings that you may have.
Press Ctrl + W and then (after releasing Ctrl + W) press the arrow keys to change the pane.
It is very useful to use set mouse=a in your .vimrc file. It gives you possibility to switch between windows using mouse. Additionally you can resize windows using it.
If you prefer to use keyboard I have also mapped arrow keys in .vimrc in this way:
map <C-Left> <C-W>j
map <C-Down> <C-W>k
map <C-Up> <C-W>h
map <C-Right> <C-W>l
To move among left and right pane, Press ctrl+w and then ctrl+r. This is both left and right vice-versa.
You can also use :wincmd w for next window, and :wincmd W for previous window.
The :wincmd is especially useful when ctrl+w is captured by the environment. For example see: https://stackoverflow.com/a/73749587/811335

Vim mappings/command not working

I am working with split windows in vim, but I am having trouble navigating and resizing split windows.
I have added these mappings to my .vimrc text file:
nmap <silent> <A-Up> :wincmd k<CR>
nmap <silent> <A-Down> :wincmd j<CR>
nmap <silent> <A-Left> :wincmd h<CR>
nmap <silent> <A-Right> :wincmd l<CR>
But still, when pressing <A-Left> it does not move window. Also I am using <C-w> +, but the windows do not resize! I also tried <C-w> <, but it resizes like 1 pixel at a time! Is there any faster way to resize split windows?
10 Ctrl-W+ increases the window size by 10 lines. To resize all Windows to the same size, you can use CTRL-W = and to increase a window to its maximum size, use Ctrl-W _.
To resize in different steps, you can create maps that will adjust the window size differently. For example to increase the window size by a factor of 1.5 and decrease the window size by 0.67, you can map this:
nnoremap <silent> <Leader>+ :exe "resize " . (winheight(0) * 3/2)<CR>
nnoremap <silent> <Leader>- :exe "resize " . (winheight(0) * 2/3)<CR>
Alternatively you can use the :resize command to change the height of the window, to change the window width, use the :vertical modifier. So to resize by 10 lines, use:
:res +10
:res -10
as for navigation try
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
The <C-W> commands resize by individual lines / columns (pixels wouldn't even be possible in console Vim), but you are supposed to prepend a number before them to change larger amounts at once. (A lot of Vim commands take such a [count], as it's called in the help.)
For the <A-Up> mappings, do they work in graphical GVIM, but not in the console?! The mapping definitions look fine (but you should use :nnoremap).
Due to the way that the keyboard input is handled internally, some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.
Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8
But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

Remap Ctrl-ArrowKeys to switch between split buffers

How can I remap Ctrl-ArrowKeys to move between split buffers, like I can with <C-w> <ArrowKeys>?
Right now <C-Right> deletes until the end of line and goes into insert mode - which is not what i prefer (I use C for this) and would be happy to get rid of this behavior.
nnoremap <silent> <C-Right> <c-w>l
nnoremap <silent> <C-Left> <c-w>h
nnoremap <silent> <C-Up> <c-w>k
nnoremap <silent> <C-Down> <c-w>j
It must be said that not all mappings work on all terminal, so you mileage may vary. This is working well with gvim.
I also use those mapping when working with spli windows:
" To move between buffers and maximise the selected one
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Combined with
" This sets the minimum window height to N
set wmh=0
It's pretty good.
I have been used this for a while now and I'm completely addicted!

Resources