Omnipresent
Omnipresent

Reputation: 30384

easier way to navigate between vim split panes

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.

Upvotes: 97

Views: 84137

Answers (8)

Paul Rougieux
Paul Rougieux

Reputation: 11399

When using vim inside a browser ctrl + w is problematic because it causes the browser tab to close. I sometimes use vim this way when connecting to a server through Apache Guacamole.

:wincmd

Vim's :help wincmd page explains that :wincmd or its shortcut :winc can be used to move between splits:

Note: All CTRL-W commands can also be executed with :wincmd, for those places where a Normal mode command can't be used or is inconvenient.

For example to move left, down, up, right:

:winc h
:winc j
:winc k
:winc l
  • Inspired by this answer on vi stack exchange https://vi.stackexchange.com/a/3737/6671

  • As explained by overthink in another answer, another solution is to remap ctrl h to move to the left split and similarly with the down, up and right moves by entering the following in your .vimrc

    tnoremap <C-J> <C-W><C-J>
    tnoremap <C-K> <C-W><C-K>
    tnoremap <C-L> <C-W><C-L>
    tnoremap <C-H> <C-W><C-H>
    

Upvotes: 1

TeraByte
TeraByte

Reputation: 111

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.

Upvotes: 4

Nafaa Boutefer
Nafaa Boutefer

Reputation: 2359

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.

Upvotes: 39

AMOL MISHRA
AMOL MISHRA

Reputation: 77

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

ctrl+w ctrl+w

Upvotes: 4

overthink
overthink

Reputation: 24443

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>

Upvotes: 122

Dmase05
Dmase05

Reputation: 1089

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>

Upvotes: 2

deadghost
deadghost

Reputation: 5217

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>

Upvotes: 23

Sam Brinck
Sam Brinck

Reputation: 901

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.

Upvotes: 3

Related Questions