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.