Navigating between VIM windows in an alternative way

25,436

Solution 1

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 ...

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
25,436
Adas
Author by

Adas

Updated on September 22, 2020

Comments

  • Adas
    Adas over 3 years

    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.