Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disallow subsequent h/j/k/l

Tags:

vim

I want to force myself to not press jjjjj and rather use 5j instead. I'm looking for a solution that forbids / disables that kind of subsequent motion usage.

For initially practicing h/j/k/l instead of arrows I used

nnoremap <Left> :echoe "Use h"<CR>
nnoremap <Right> :echoe "Use l"<CR>
nnoremap <Up> :echoe "Use k"<CR>
nnoremap <Down> :echoe "Use j"<CR>

I tried to do something similar like

nnoremap jj :echoe "Use xj"<CR>
nnoremap ll :echoe "Use xl"<CR>
nnoremap kk :echoe "Use xk"<CR>
nnoremap hh :echoe "Use xh"<CR>

But this results in that even jumping with 5j needs to wait for the vim timeout.

like image 266
patchrail Avatar asked Sep 01 '15 06:09

patchrail


2 Answers

I've checked vim-hardtime, but it also prevents me from doing things like 2j9j within the timeout, which I would hardly call a bad habit, but rather a sudden change of mind while navigating.

The following might be a starting point (to be put in your .vimrc file) from which you can develop your own plugin:

nno <silent> j :<C-U>execute "call Restrictedj(" . v:count . ")"<CR>
let g:moved1 = v:false
fu! Restrictedj(count)
  if a:count > 1
    exe line('.') + a:count
    let g:moved1 = v:false
  else
    if !g:moved1
      exe line('.') + 1
    else
      echoe 'Use xj'
    end
    let g:moved1 = v:true
  end
endf

Such a code will make j (without count) error from the second use of it on.

The main fault is that you can only reactivate it by pressing 2j, 3j, or more, and not by pressing any other key (which would be desirable).

In principle the function can be modified in such a way that pressing each one of the four hjkl reactivates the remaining three. However I think that the ideal is that each of hjkl should be reactivated by any action other than pressing that key again.

like image 62
Enlico Avatar answered Nov 06 '22 03:11

Enlico


The timeout is unavoidable by definition, but you could at least reduce the timeout by setting timeoutlen. It defaults to 1000, which is quite long. You could probably get away with lowering it to 500, especially seeing as you are planning on using this only temporarily as a training aid.

like image 1
Greg Hurrell Avatar answered Nov 06 '22 04:11

Greg Hurrell