32

I am using CtrlW to navigate between VIM split windows. Does there exist any different ways to do this?

For example, if I have, say, 5 split windows opened and want to navigate to the top left corner window, CtrlW is very uncomfortable as it requires many keystrokes.

Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
Adas
  • 345
  • 1
  • 3
  • 9

5 Answers5

51

Why not setup something like these?

nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

Much quicker ...

Rook
  • 60,248
  • 49
  • 165
  • 242
  • 1
    Thanks. I'm aware of this solution but I was searching for something what would allow to navigate to a specific window. Anyways, this should be a satisfiable solution in case no alternatives exists. – Adas Jan 28 '12 at 18:08
  • @Adas - There is no better way, not that I'm aware of. The thing is, you don't navigate through windows (they're just ... viewpoints). You navigate through buffers. So if you have a 2x2 window configuration, the upper left will not necessarily be the 1st, upper right the second and so on ... – Rook Jan 28 '12 at 19:59
  • 4
    I have `nnoremap w`, since I never use the return key in normal mode. Then it is effortless to cycle through the splits. – Prince Goulash Jan 30 '12 at 08:55
  • @PrinceGoulash - Except you have to hit it 3 times if you got 4 splits, and so on ... but okey – Rook May 16 '12 at 15:34
  • This is a little old, but for anyone who ends up here, it could be worth checking out [minibufexpl.vim](http://www.vim.org/scripts/script.php?script_id=159). Easily edit multiple files in "tabs" – Marty Mulligan May 28 '14 at 17:55
  • 1
    Combine PrinceGoulash's mapping with skeept's answer and all you have to do is type `` to go to any split instantly – abeger Apr 04 '20 at 20:31
26

You can use <number><c-w>w to switch to a particular window. So 1<c-w>w goes the the first window (top left corner) 11<c-w>w moves to the last window (here I assume you have less than 11 splits).

I also find the following mappings convenient and have them in my .vimrc

nnoremap <tab> <c-w>
nnoremap <tab><tab> <c-w><c-w>

which I use for window stitching (for some reason if I don't define the second mapping if I hit tab twice I get a message "no identifier under the cursor)

Reading the help page for CTRL-W, there is even a more convenient way than 1<c-w>w and 11<c-w>w to go to the first and last window: <c-w>t goes to the top window and <c-w>b goes to the bottom window.

skeept
  • 12,077
  • 7
  • 41
  • 52
5

You can use the nnoremap command in your vimrc to use custom keybindings.

The syntax of nnoremap is this:

nnoremap new_keybinding keystrokes

The nnoremap command assigns a new keybinding that, when you press it in normal mode, the sequence of keystrokes that have been assigned to this command are echoed to Vim.

EDIT: There's also the nmap command. The difference between the two is that nmap allows to overwrite your current keybindings, while nnoremap does not. The difference between them is explained in this answer.

For example, I have put these lines in my .vimrc:

"Better window navigation
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-h> <C-w>h
nnoremap <C-l> <C-w>l

This allows me to use Ctrl+j, Ctrl+k, Ctrl+h, Ctrl+l instead of Ctrl+W j, Ctrl+W k, Ctrl+W h, Ctrl+W l for window navigation, while retaining the old keybindings.

You can also look up :help key-mappings for more information.

Community
  • 1
  • 1
Alexandros
  • 3,044
  • 1
  • 23
  • 37
  • 1
    Regarding nmap/nnoremap - no, that is not the difference. You can overwrite your nnoremap mappings just like any other, The difference is that nnoremap is not recursive. – Rook Jan 28 '12 at 17:47
2

Thanks to this answer on the SE site dedicated to Vim, I came up to another alternative which uses a plugin to create a submode dedicated to windows management. It means that with a combination of keys I enter a new mode in which some keys will allow me to do different actions on the windows.

After installing vim-submode let's add some lines to our vimrc to configure a new mode:

" Create a submode to handle windows
" The submode is entered whith <Leader>k and exited with <Leader>
call submode#enter_with('WindowsMode', 'n', '', '<Leader>k', ':echo "windows mode"<CR>')
call submode#leave_with('WindowsMode', 'n', '', '<Leader>')

Now you simply have to press Leader+k to enter the new mode (You can change this with the line submode#enter_with) and press Leader to exit it.

" Change of windows with hjkl
call submode#map('WindowsMode', 'n', '', 'j', '<C-w>j')
call submode#map('WindowsMode', 'n', '', 'k', '<C-w>k')
call submode#map('WindowsMode', 'n', '', 'h', '<C-w>h')
call submode#map('WindowsMode', 'n', '', 'l', '<C-w>l')

With these lines, after you entered the new mode (with Leader+k) you'll be able to move between your windows with the keys hjkl as if you were using <c-w>hjlk in normal mode.

" Resize windows with <C-yuio> (interesting on azerty keyboards)
call submode#map('WindowsMode', 'n', '', 'u', '<C-w>-')
call submode#map('WindowsMode', 'n', '', 'i', '<C-w>+')
call submode#map('WindowsMode', 'n', '', 'y', '<C-w><')
call submode#map('WindowsMode', 'n', '', 'o', '<C-w>>')

Some few more lines to allow the resizing of the window with yuio (I choose these keys because on an azerty keyboard they are just on the row over hjkl and are pretty convenient to use, maybe it would be more useful to change that on a qwerty keyboard, Im not sure).

" Move windows with <C-hjkl>
call submode#map('WindowsMode', 'n', '', '<C-j>', '<C-w>J')
call submode#map('WindowsMode', 'n', '', '<C-k>', '<C-w>K')
call submode#map('WindowsMode', 'n', '', '<C-h>', '<C-w>H')
call submode#map('WindowsMode', 'n', '', '<C-l>', '<C-w>L')

Let's move the windows with <C-hjkl>.

" close a window with q
call submode#map('WindowsMode', 'n', '', 'q', '<C-w>c')

" split windows with / and !
call submode#map('WindowsMode', 'n', '', '/', '<C-w>s')
call submode#map('WindowsMode', 'n', '', '!', '<C-w>v')

And some more mappings to close a window and create new splits.

let g:submode_keep_leaving_key = 1
let g:submode_timeout = 0

Finally these options allow to keep a key pressed and it will repeat its action.

Note I am aware that this answer describe more than just navigating between windows as OP was asking. I think that creating a submode is pretty convenient but is only interest if the submode allows to do more than just one action.

Community
  • 1
  • 1
statox
  • 2,827
  • 1
  • 21
  • 41
0

You can create diagonal movements by continuing <C-w> a second time in another direction.

  "move from bottom left to top right diagonally
  "Mnemonic: keyboard finger movement from j to i
  nnoremap <C-i> <C-w>l<C-w>k 

  "move from top right to bottom left diagonally
  "Mnemonic: keyboard finger movement from j to n
  nnoremap <C-n> <C-w>h<C-w>j 

  "move from  top left to bottom right diagonally
  "Mnemonic: keyboard finger movement from j to m
  nnoremap <C-m> <C-w>l<C-w>j

  "move from bottom right to top left diagonally
  "Mnemonic: keyboard finger movement from j to y
  nnoremap <C-y> <C-w>h<C-w>k 
zundarz
  • 1,540
  • 3
  • 24
  • 40