iTerm: word shortcuts

In a terminal, I need both the “option” key to access alternate glyphs, like “•”, “∞” and French accents, like “é”, and to act like a “meta” key, which essentially sends an “escape” character followed by the key value.

Obviously a key can’t be two things at the same time, at least on Os X. On other Unix-like systems, you often have the left “alt” playing the role of the meta key, and the right “alt” playing the “altgr” role.

Some of the shortcuts I use the most are word navigation, using meta-left-arrow and meta-right-arrow, and word deletion, with meta-backspace (or “meta-delete” on Macs) to delete the word on the left, and meta-d to delete the word on the right.

So here are my recipies to bind those keys in iTerm.

What the heck does a “meta” key?

In most terminals, it simply sends the key you press while holding it, preceded with an “escape” character. So if you hit “esc”, then “delete”, it’ll delete the word on the left of the cursor. But deleting, or moving 10 words in a row is quite painful that way.

Holding the meta key, and hitting madly “delete” is much funnier!

The key mappings…

Essentially, you go to the “Bookmarks” menu, then select “Manage Profiles…”. In the window that pops up, open “Keyboard Profiles”, select “Global” (or whatever profile you normally use).

Then add a new key mapping by clicking on the “+” button.

Move one word right

Field Value
Key cursor right
Modifier Option
Action send escape sequence
Value f
High interception priority no

Move one word left

Field Value
Key cursor left
Modifier Option
Action send escape sequence
Value b
High interception priority no

Delete the word on the right

Also known as “forward-kill-word”. It’s generally present on two key bindings: alt-d and alt-del (“del” on a Mac is “delete” on other computers, the key above the arrows on a full keyboard).

option-d:

Field Value
Key hex code, “0x64” in the box
Modifier Option
Action send escape sequence
Value d
High interception priority yes

option-del:

Field Value
Key del
Modifier Option
Action send escape sequence
Value d
High interception priority no

Delete the word on the left

iTerm makes this a bit tricky because it doesn’t allow you to specify a character by its hexadecimal (or decimal, or octal) value. So you can’t send “\e\x7f” or such.

You could define it so it outputs a non-standard sequence, then map it in you .inputrc file (and in my case in the .inputrc file on many linux servers, and then in a bunch of applications that don’t use inputrc). But… there are other ways.

The first, and probably the less accessible way, is to apply a patch I posted earlier (see iTerm: extend the “send text” action and compile iTerm yourself. But let’s not delve into this.

The second is to paste the 0x7f character, which is normally associated with the “backward delete” (named “delete” on Macs, and “backspace” on PCs).

Pasting a character who’s goal is to delete the character on the left may seem a bit daunting, but there are ways!

Field Value
Key delete
Modifier Option
Action send escape sequence
Value see below
High interception priority no

The “see below” value you have to enter is the pure character 0x7f. There are many ways to insert that invisible character.

Using the “Unicode Hex Input” keyboard

Go to the system preferences and add the “Unicode Hex Input” keyboard. In the text area, hold option and type 007f (while holding option). You won’t see anything, but it should be there.

Using the “Characters” palette

If you have the keyboard icon in the menu bar, select “Show Character Viewer”. In “View”, select “Code Tables”, then “Unicode”, then the line “Basic Latin”. In the character array below, on the “0070” line, the last character, in the “F” column, just right of the tilde (“~”)… it’s invisible. Select it by clicking one time on it. The background should turn blue.

Select the text input in iTerm’s key mapping window, then, in the Characters palette, click "insert".

Using UnicodeChecker

UnicodeChecker is a software similar to the Character Palette, but different…

In its window, scroll to “U+007F”. Then click in the huge blank box on the right, select all (command-a), copy (command-c), then paste it in iTerm’s text box.