49

I finally decided to ditch the arrow keys for navigation in favor of hjkl. I used the Vim hardmode plugin to disable the arrow keys.

I got used to navigation pretty fast, but it turns out that navigation in insert mode suddenly is impossible.

Is there any solution for this? (exiting insert mode is not a solution).

4
  • 10
    Yes: use the arrow keys :-) That's what they're there for ;-) Nov 15, 2016 at 19:02
  • 1
    @Carpetsmoker if the arrow keys are available I would use them instead of hjkl....
    – stdcall
    Nov 16, 2016 at 6:51
  • 6
    But the idea is that exiting insert mode (namely, using it just for what it's meant for, i.e., small insertions interspersed with other commands) exactly is the solution. If I am not mistaken I've seen it in the help, if I find a link I'll post it. Seems that you've got used to that too much, not to the arrow keys.
    – The Vee
    Nov 16, 2016 at 10:54
  • You can bind your keyboard to use arrows as a shortcut like <kbd>super</kbd> + <kbd>j</kbd> to move down. <kbd>super</kbd> can be any button, for example <kbd>Caps Lock</kbd> is pretty common Nov 12, 2019 at 11:26

5 Answers 5

48

There are many options you have. One option, and IMO the sanest and easiest is to just stop disabling the arrow keys. I understand why many hardcore vimmers say things like

You should never ever use the arrow keys again! Every time you use the arrow keys, you waste 3 minutes of time, a puppy dies and somebody switches to Emacs!

(Obviously I'm kidding) However, there is a purpose for the arrow keys. They're extremely useful for navigating:

  • In insert mode

  • In the command line

Generally when people tell you not to use the arrow keys, it's because they want you to relearn navigation in a more vim-like way. If you are comfortable with hjkl, and you mostly use the arrow keys in insert mode, there's no good reason to force yourself not to use them.

Option #2 is to remap ctrl-hjkl to act like cursor keys. This is nice because you can stay on the homerow, and it fits well with the muscle memory you have already learned using hjkl in normal mode. In fact, this is what I do. The major downside to this approach is that in can be a pain to setup in terminal vim because <C-j> produces a NUL byte. I get around this by using Gvim. I am not aware if there is an easy fix for console vim or not.

Option #3 is to use <C-o> to make small normal mode movements. For example, if you are typing

Hello world!
     cursor^

and you realize you forgot a comma, rather than doing leftleftleftleftleftleft, you could simply do <C-o>b<C-o>h or <C-o>2Fo, or any other combination of normal mode movements you like.

Of course, another option, and one I like very much is to just use Esc, make a few small movements before jumping back in to insert mode. You mentioned that you'd rather not do that, but sometimes this really is the most efficient way.

There are probably some other approaches too, but these are the ones I like that have worked for me.

7
  • 2
    Interesting--I note you use <c-o>gk and <c-o>gj instead of <Up> and <Down>. Why is that? Nov 16, 2016 at 0:42
  • 4
    @kylestrand gk and gj visually go up and down a line even if the line is wrapped.
    – DJMcMayhem
    Nov 16, 2016 at 0:44
  • 5
    I don't think this is enough to warrant an answer all on its own, but I like having the arrow keys disabled in normal mode (which is what forced me to learn HJKL), and enabled in all other modes (noremap <Up> <NOP>...). You spend enough time in normal mode to learn HJKL, but they're there when you need it. Or at least, I find this to be a comfortable middle ground between leaving them always off or always on, and it doesn't require remapping other keys (a lot of people like Ctrl-HJKL for moving between panes). Nov 16, 2016 at 1:00
  • Btw, BASH et al support vi key bindings. With these mappings, you can <ESC> and use hjkl to go through cmd history (up/down) or modiy the current command (left/right)
    – Rolf
    Nov 23, 2016 at 8:34
  • Just as a side note - all this changes if we talk about Colemak, Dworak or custom split keyboards.
    – Dzintars
    Dec 10, 2021 at 10:27
19

I like to use the Control key to turn the hjkl movement keys into "universal" movement keys. Here's the relevant bit of my .vimrc:

" In insert or command mode, move normally by using Ctrl
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
cnoremap <C-h> <Left>
cnoremap <C-j> <Down>
cnoremap <C-k> <Up>
cnoremap <C-l> <Right>

The inoremap statements apply to insert mode, while the cnoremap ones apply to command mode. (I've been using this mapping for so long that I have forgotten whether there's any way to navigate command-mode history without it.)

(Edit: I see the other answer has already mentioned this possibility. Still, it may be helpful to see the actual mapping commands.)

7

Hit esc then use hjkl and wWbB and any other navigations you would like to make. The whole point of hard mode is to get you to learn to navigate with more complex vim motions. Make small edits then move to a new location, using normal mode.

If you wan't to learn how to navigate faster with hardmode enabled, check out my answer from another question on the same topic.

1
  • This is not what the question is about ;) We would know this already.
    – windmaomao
    Aug 19, 2022 at 13:47
7

If you just need to modify a small amount of text or a word, you can use <Ctrl-O>h/j/k/l.

<Ctrl-O> takes you back to Normal mode just for that command, and then drops you back into insert mode, so it saves you some amount of key presses, if you need to go into normal mode for a small movement.

Ref: 1. http://vim.wikia.com/wiki/Use_Ctrl-O_instead_of_Esc_in_insert_mode_mappings

4

If you want to move more than ONE or TWO positions...

...the best choice is to hit ESC, move around, and get back to insert mode again.

There's no point on create mappings like <C-h> to move left and then start hitting it too many times... as a vim user you're not supposed to hit the same key multiple times to achieve a smart movement.

(If the ESC key isn't close to your fingers, it would be a good option to create a mapping for it.)

If you want to move ONE or TWO posistions on insert mode...

...a good choice would be to define some movements using your <Leader> key:

(I use , as <Leader> key since it's feels close and confortable to my fingers)

noremap! <Leader>h <left>   "move cursor left
noremap! <Leader>j <down>   "move cursor down
noremap! <Leader>k <up>     "move cursor up   
noremap! <Leader>l <right>  "move cursor right
noremap! <Leader>w <esc>wi  "move one word forward
noremap! <Leader>e <esc>ei  "move forward to the end of word
noremap! <Leader>b <esc>bi  "move one word backward
1
  • You are correcting your own message "as a vim user you're not supposed to hit the same key multiple times to achieve a smart movement.", but your leader key is exactly doing that, man
    – windmaomao
    Aug 19, 2022 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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