21

I want to teach myself using hjkl more than using the arrow keys in order to stay on the home row. And yes, I am very aware that one should not use hjkl as the main navigation keys. Yet my habits are quite strong and I don't even realize that made that hand movement to the arrow keys and I want to unlearn it.

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

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

1

You could add this to your .vimrc to disable the arrow keys in Command, Insert, Normal and Visual Mode:

" Remove newbie crutches in Command Mode
cnoremap <Down> <Nop>
cnoremap <Left> <Nop>
cnoremap <Right> <Nop>
cnoremap <Up> <Nop>

" Remove newbie crutches in Insert Mode
inoremap <Down> <Nop>
inoremap <Left> <Nop>
inoremap <Right> <Nop>
inoremap <Up> <Nop>

" Remove newbie crutches in Normal Mode
nnoremap <Down> <Nop>
nnoremap <Left> <Nop>
nnoremap <Right> <Nop>
nnoremap <Up> <Nop>

" Remove newbie crutches in Visual Mode
vnoremap <Down> <Nop>
vnoremap <Left> <Nop>
vnoremap <Right> <Nop>
vnoremap <Up> <Nop>

Note: Checkout :help map-overview for more details on the different types of mapping

  • 1
    You can reduce the number of necessary commands by using noremap ... ... (for normal, visual, select and operator-pending mode) and noremap! ... ... (for insert and command-line mode). – Jürgen Krämer Jul 10 at 6:30
  • True. I thought the more explicit noremaps would be helpful for someone wanting to customize what modes are affected – NonlinearFruit Jul 10 at 12:25

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.