21

I want to teach myself using hjkl more than using the arrow keys in order to stay on the home row.

Yet my habits are quite strong and I don't even realize that made that hand movement I want to avoid.

In order to break that habit: How can I disable the arrow keys so that I am forced to use the hjkl keys?

As a bonus, it would be helpful if there was a message shown Dont't use this! yet that it's not mandatory. If they don't work, it will be confusing enough for me to rethink my habits.

  • 4
    There's absolutely no point doing that; HJKL suck just as much as the arrows. – romainl Dec 17 '15 at 7:00
  • 5
    I think @romainl may be pointing out that it's more important to focus on using vim's powerful motions (like w for next word, } for next paragraph, n for next search result match, fz for next occurrence of z in the current line, etc.) than moving more granularly by single characters. I definitely agree with this. However, HJKL are definitely better than the arrow keys in that you don't have to move your hands from the home row. I'll frequently type something like jA, which is a lot faster than <Down>A, and not really beatable by any motion. – wchargin Dec 17 '15 at 7:45
  • 4
    @romainl: I think I had this discussion with you before. Non-vimmers are used to arrows, and not used to HJKL. Ideally you don't want to use either; but blocking arrows (and not HJKL) will make people pause and reflect on what they are doing (while still leaving HJKL open as an alternative in case it is actually the best option, or in case the user is stumped on how to do it with more powerful motions.). I.e. it is not about learning HJKL, it is about blocking the automatism learned in other editors. – Amadan Dec 17 '15 at 8:19
  • 1
    @Carpetsmoker What is definitely an upside of the hjkl keys is not having to leave the home row. For the arrow keys the hand has to move across the keyboard. – k0pernikus Dec 17 '15 at 13:12
  • 1
    @Carpetsmoker It's surely feels better to me. ;) – k0pernikus Dec 17 '15 at 13:25
21

You can install the hardmode plugin and in your .vimrc put in

let g:HardMode_level = 'wannabe'
let g:HardMode_hardmodeMsg = 'Don''t use this!'
autocmd VimEnter,BufNewFile,BufReadPost * silent! call HardMode()

If you don't want to use a plugin (which may be a better choice, as you get to customise everything yourself!), use nnoremap, vnoremap and inoremap on <Left>, <Right>, <Up>, <Down>, <PageUp> and <PageDown>, for example:

nnoremap <Left> :echo "No left for you!"<CR>
vnoremap <Left> :<C-u>echo "No left for you!"<CR>
inoremap <Left> <C-o>:echo "No left for you!"<CR>
  • 1
    There's also <nop> if you want to disable the keymap altogether: nnoremap h <nop>. – akshay Dec 17 '15 at 8:09
23

In case you, or someone else reading this topic, just wants to disable the key movements without the text warning enter the following lines in .vimrc

noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>

The commands will only disable the key movement in normal mode. They still work in insert mode. If you use inoremap the arrow movement will be removed from insert mode as well.

This massively popular topic deals with the differences between noremap, nnoremap, inoremap etc: Remapping in Vim

The general idea with remapping goes like this:

Choice-of-mode-to-remap <remap this command> <to this action/command instead>

So in the other examples given you remap your left command to a text prompt which incidentally removes the action of actually moving the cursor. The example I gave above simply remaps the actions do to nothing, rather than something else. Consider it removing, rather than replacing, vim functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.