Search code examples
vimneovimtouch-typing

How do I rebind `hjkl` to `jkl;`


I've been trying to rebind to proper touch typing keys, and it's actually more complicated I expected. This is my init.vim:

" Normal mode
nmap ; <Right>
nmap l <Up>
nmap k <Down>
nmap j <Left>
nnoremap h ;

" Visual mode
vmap ; <Right>
vmap l <Up>
vmap k <Down>
vmap j <Left>
vnoremap h ;

" Rebind the window-switching movements
nnoremap <C-w>; <C-w>l
nnoremap <C-w>l <C-w>k
nnoremap <C-w>k <C-w>j
nnoremap <C-w>j <C-w>h
nnoremap <C-w>h <C-w>;

Looks fine, right? Except it's not. By default in vim, when you press Ctrl + W + k, your window will switch, regardless if you pressed k with Ctrl + W already being pressed down or in succession to Ctrl + W. However, with my key rebinds, the movement key must be pressed after releasing Ctrl + W. This ruins my workflow, as sometimes, I try to quickly switch window, and I fail because I didn't release Ctrl + W quick enough.

How can I achieve a proper keybind without making window-switching less convenient? Thanks.


Solution

  • If you look at :help CTRL-W_j and its friends, you will see that they all have a bunch of alternatives. The important one is <C-w><C-j>, which is what lets you keep your left pinky on Ctrl while you press j with the right index or keep your left pinky and index on Ctrl and w while you press j with the right index.

    Therefore:

    [...]
    nnoremap <C-w>j <C-w>h
    nnoremap <C-w><C-j> <C-w>h
    [...]