8

In Vim it is nice to use hjkl in normal mode and would be great to continue to use them in insert mode. I tried to map them to Ctrl-h, Ctrl-j, Ctrl-k, Ctrl-l:

 imap <C-h> <left>
 imap <C-j> <down>
 imap <C-k> <up>
 imap <C-l> <right>

but it is not convenient especially because it masks Ctrl-H and backspace stops responding. Have you been able somehow to use HJKL keys for movements in insert mode?

dimus
  • 8,712
  • 10
  • 45
  • 56
  • 5
    Why do you want to use normal commands in insert mode? Just [hjkl]+i. Maybe Emacs would be more convenient for you if you want this kind of behavior. – alternative Aug 21 '10 at 16:59
  • 1
    Or use the arrow keys...they actually make more sense for moving the cursor around. :P – cHao Aug 21 '10 at 17:00
  • 4
    Seriously. Insert mode is for inserting *only* -- if you spend all your time in insert mode you're doing it wrong. – Josh Lee Aug 21 '10 at 17:02
  • I do use vim for more than 10 years as my editor of choice for about everything, and I still feel it would be nice not to go to arrow keys in insert mode. – dimus Aug 21 '10 at 17:20
  • 1
    I have a similar complaint: when I am in Vim's command line, I would like to navigate the history using j/k, but of course it's like being in insert mode, and remapping is not a good idea (I had loads of problems when I tried doing that!) – UncleZeiv Aug 21 '10 at 23:36
  • This reminds me of when I'm composing an e-mail, typing in some text field, or using Windows for some reason and I notice something like `:wkhhxi` show up in the text field. Quite disorienting. – redbmk Apr 10 '13 at 01:14
  • 2
    @UncleZeiv, did you know you _can_ browse the history using j/k/h/l? Try typing `q:` or `q/` to bring up your last few commands or searches and from there you can browse as if it's another vim window. Edit any of the lines, hit enter, and it'll run the command. To get out of there without running one of the commands just hit `:q`. I ran into this countless times on accident before I figured out what it was actually for. – redbmk Apr 10 '13 at 01:36
  • @UncleZeiv, you might also be interested in bash's vim command line mode. http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/ – redbmk Apr 10 '13 at 01:39

4 Answers4

7

I'm using double upper case mapping in insert mode for various mapping. That works pretty well, except when you are pasting text from somewhere. It's usually wiser to clear all insert mappings before inserting text.

So you can try

imap HH <left>
imap JJ <down>

etc...

Obviously , you will need twice key strokes as the normal move, so I guess if you need to navigate "far away" it's better to go back in navigation mode.

mb14
  • 22,276
  • 7
  • 60
  • 102
  • 2
    You can also type `:set paste!` to toggle paste mode on and off. Paste mode disables imaps, autoindents, etc so that you can conveniently paste anything. You could create a macro like `map ,p :set paste!` to make that easier, too. Then `,p` would turn on paste mode (even if you're in insert mode) and you would just need to hit `,p` again from normal mode to turn it back off. – redbmk Apr 10 '13 at 01:01
3

I have mapped my normal mode arrow keys to 'bubble' text around. It is very useful for moving blocks of code or text around with minimal effort.

" Useful bubble text normal mapping for arrow keys.                                                                                        
nnoremap <UP> ddkP 
nnoremap <DOWN> ddp
vnoremap <DOWN> xp`[V`]
vnoremap <UP> xkP`[V`]

Definitely inspired by Drew Neil's episode on Bubbling Text on VimCasts.org.

Tom
  • 15,798
  • 4
  • 37
  • 48
2

Pardon me for saying so, but how useful would be to have them work as in normal mode. I mean, what would you use for typing hjkl then?

But, if you really want to map them, sure, just map them with <imap> <something-h> <some operation> and off you go.

Although I see no point. If you want to keep your hands on the middle of the keyboard, why not map jj to <Esc> (some prefer that way, so they don't have to move their fingers off the home row).

Rook
  • 60,248
  • 49
  • 165
  • 242
  • I do have jj mapped to esc actually, and finding a convenient that does work well is my question – dimus Aug 21 '10 at 17:17
  • @dimus - Well, there aren't that many of options. If we take the assumption that you won't be mapping the hjkl in insert mode to movement, then you're stuck (on most machines) with Ctrl, Alt and Shift (which is also impractical) combinations. Mind you, I still don't see why would you want movement in insert mode (as a 10 years old vimmer, I gather you would've gotten used to "write in insert mode, everything else in N and E modes" routine, but to each his – Rook Aug 21 '10 at 18:06
  • own. In vim practically you can map anything that gives a signal when pressed in combination with something else (that means no, for example), so ... the rest is up to you. – Rook Aug 21 '10 at 18:06
  • Map function keys if you're using a laptop (they're "easier" to reach then arrow keys on most laptop keyboards) for example. – Rook Aug 21 '10 at 18:07
  • I do use movement keys in insert mode when it feels faster than going to normal mode and back to insert mode, that is my use case. – dimus Aug 23 '10 at 15:29
  • @dimus - Well, anyways ... I gave you your options. There aren't many of them, all together. You can either use Vim in the way it was ment to be used (that persumes movement and pretty much everything else 'cept the typing, in N mode), or try to modify it to yourself with the few combinations you've got. In my experience, the more people try to modify Vim out of the way it was designed, the more problems they have with it, and after a time, they usually move to something else (Emacs? Notepad++?) ... – Rook Aug 23 '10 at 16:05
0

I was trying to do the same. I found the answer in the following link: Traversing text in Insert mode.

For some reason still unknown to me, the Ctrl binding in insert mode didn't work for me. One alternative is by pressing Ctrl-O which switches to normal mode for one command, so <C-o>h, <C-o>j, <C-o>k, <C-o>l will let you move around while in insert mode. Alternatively, I have used the following mapping as I find it shorter than pressing <C-o> plus h,j,k,l:

inoremap h<Tab> <Left>
inoremap j<Tab> <Down>
inoremap k<Tab> <Up>
inoremap l<Tab> <Right>
Community
  • 1
  • 1
iDuran
  • 379
  • 3
  • 5