Last Updated: October 04, 2020
·
16.58K
· amoniker

Vim movement in iTerm2

If you like to edit code with Vim (or if you've played too much nethack) then you probably enjoy the convenience of moving the cursor around with the h j k l keys.

In iTerm2, it's pretty simple to map those keys to behave the same way. In my case, I decided that whenever I held down Control and pressed h j k l then I should get Vim movement in my terminal.

To set this up

  • go to Preferences > Keys in iTerm2
  • click the plus button at the bottom to new Global Shortcut Keys for each mapping
  • Enter the keyboard shortcuts you'd like to use, such as control+h and so on
  • Set the Action to Send Escape Sequence and set the Esc+ field to one of the following for each arrow key:
Up: [A
Down: [B
Right: [C
Left: [D

The escape sequences were found using sed -n l then pressing each arrow key. The ^[ prefix is automatically added by iTerm2 keymapping, so the last two characters in the escape sequence are all that's necessary.

If you happen to use Sublime Text 3 and you'd like to enable this sort of movement, check out my protip here: https://coderwall.com/p/rvekkq

Edit: If you want this for any application, try http://www.keyboardmaestro.com - I set these keys up in Chrome as well :)

Enjoy!

Elbereth

Related protips:

Use ⌥ ← and ⌥→ to jump forwards / backwards words in iTerm 2, on OS X

3 Responses
Add your response

Are you aware of the "vi mode" in bash? You get the HJKL movements and much more with set -o vi.

It starts in the insert mode, if you press ESC you can use most vi key bindings.

over 1 year ago ·

@ricobl, I didn't know about vi mode in bash! That's pretty cool. I might use that in combination with my new control-key movement since I should learn more vim shortcuts.

For now I ended up setting more shortcuts with Keyboard Maestro so I can toggle between split terminals and tabs, and scroll up and down in the buffer.

over 1 year ago ·

This works great for me! It's more useful than vim bindings since I'm using colemak. Thanks!

over 1 year ago ·