Shreyas Jani

I'm a web designer & software engineer based out of Mumbai.

I write about technology & love documenting the world around me through photography.

Jump around words using arrow keys in iTerm

24 May 2020

I've been using MacOS as my primary development machine for around 2 years. iTerm2 quickly became my favorite commandline editor. Before moving to MacOS, I was heavily reliant on terminator on Ubuntu. Navigating between words using ctrl + ←/→ was a breeze. iterm on MacOS doesn't support that functionality out of the box.

I was okay with not being able to jump around words on iterm as I wasn't running long commands on the commandline. This went on fine till I had to use git and modify a command or add a flag to it.

The Problem

Case in point, I was adding a couple of npm packages for webpack and had chained a couple of package names to the same command. I misspelt optimize-css-assets-webpack-plugin as optimise. Not only that package name is big, to have chained package names following it was a nightmare already. Jumping around words like I was able to do with any fine text editor was sorely missed.

Moving one letter at a time

Deleting a whole word was also a nightmare as I had to do it letter by letter.

So here's how I've configured iterm2 on MacOS to make moving around words or deleting them less tedious.

Deleting whole words

I set the left option (⌥) key to act as an escape character. This can be done going to iTerm2 > Preferences > Profiles > Keys .

Option key as escape character

Now using the option (⌥) + backspace combination, I can delete a whole word.

Deleting words vs deleting letters in iterm

Jumping Backward and Forward

In iTerm2 > Preferences > Profiles > Keys , clicking on the plus icon lets me add a new Key Mapping. Press the key command: option (⌥) + ← select Send Escape Sequence from the dropdown and, input the character b.

I repeated this for option (⌥) + →, by using the character f.

Setting escape sequences to jump backwards and forwards

Jumping backwards and forwards, one word at a time

Using a preset

Optionally, iterm also has an option to use presets (thanks Sid for pointing this out) which kinda makes every other step outlined above moot.

Inside of iTerm2 > Preferences > Profiles > Keys , you can select the Natural Text Editing preset that has a set of key mapping that does what I've outlined above.

Using the natural text editing preset

Editing chained commands on iterm is very simple now, especially since jumping between words was second nature to me while working on the commandline.