TextMate News

Anything vaguely related to TextMate and macOS.

Word Movement in Terminal

Recently someone asked (on IRC) how to move from word to word. This is ⌥← and ⌥→ in all the Mac applications I use, except Terminal.

This prompted me to figure out how to make Terminal do it using those keys.

I have abandoned bindkey and the likes, as I work with different shells on different servers (via ssh), and even for my local zsh I wasn’t able to achieve all I wanted with bindkey (and I’m an all or nothing kind of guy :) ).

The de facto standard in shells is meta b (backward) and meta f (forward) for word movement. Terminal has a Keyboard page in the Terminal Inspector, which can be reached from Terminal → Window Settings… and is depicted below.

Terminal Key Bindings

To make option left (⌥←) move to previous word you only need to add a new key binding entry like the following:

Terminal Key Editing

To insert \033b you need to hit the escape key (which inserts \033) and then press b.

You want to do the same for option cursor right. You can also bind control left/right to begin/end of paragraph. The shell keys for these two actions are ⌃A and ⌃E. In the key bindings editor you press ⌃A (control A) and it will insert \001 as the string value:

Terminal Key Editing

Remember to hit the Use Settings as Defaults button when you are done.

Unfortunately there are pretty clear limits to what can be done. E.g. no way to make ⌥⇧← select previous word, and no way to bind actions to the backward delete key (⌫).

categories OS X Tips


07 January 2006

by Geir-Tore

I think you forgot to publish the string value for the option cursor right action?

07 January 2006

by Geir-Tore

And of course….Great tip, and thanks for solving this!!

07 January 2006

by Alain Ravet

03b = move 1 word left
03f = move 1 word right

07 January 2006

by Alain Ravet

oops. Sorry, take 2:

\33b = move 1 word left
\33f = move 1 word right

Very nice. In this same vein, you can also make the forward delete key stop being worthless and inserting ~ by mapping it to control-option-d.

Thanks for this excellent tip. That made my day.

I can’t get this to work. I’ve configured it as shown but it doesn’t work. The terminal just flashes and inserts “b” or “f” when I hit option+left or option+right. Perhaps there’s something in my bash setup which prevents it from working. Maybe this is a zsh-only thing?

On the other hand, Roland’s tip to bind forward-delete to control-option-d does work.

Option-left/right-arrow will invoke backward/forward-word in bash, tcsh, and zsh by simply setting “Use option key as meta key” on the Keyboard page of Terminal Inspector, seen in the first screencapture of this article. That also makes the option key a meta key for other useful shell functions, which is much nicer (for Emacs junkies like me) than using the escape key or control-[. Some functions, like history-search-backward/forward, are particularly painful without the metafied option key.

Unfortunately setting “Use option key as meta” isn’t possible for me because I’m using a Spanish keyboard; if I do that then I’ll lose the ability to type “~”, which tends to come in handy when using a shell!

Is this a Tiger Terminal only thing? I can’t get it to work for any of the examples in my Panther Terminal.

Ok. I’ve now got it working. The problem was that I had the following defined in my ~/.inputrc file:

ESC: menu-complete

After commenting that out it works.

This is fantastic! I use these shortcuts as second nature in cocoa apps and I’ve been frustrated they don’t work in Terminal.

Thanks for the tip!

ahhh, I’ve been looking for something like this. very nice, thanks! But I use iTerm; here is how I set it up: Preferences > Profiles … made a new Keyboard profile, associated it with my bookmarks. For forward-word, chose Action “send escape sequence” and entered “f” in the text field and “b” for backwards-word. I couldn’t however figure out how to re-map ^A and ^E. Anyone know how to tell the shell backwards-delete a word like Cocoa option+delete? :)

26 January 2006

by Clayton Hynfield

Just wanted to point out that this only works if your line-editing interface style is set to emacs. If you’re a vi weenie…well, there’s no hope for us. : D

27 January 2006

by Max Lein

Wow, that works great! Thanks a lot :-)

Holy crow, thank you. That’s been frustrating me since I moved over from Linux.

Excellent tip. Can’t believe its taken me this long to discover/learn of this! I should have snooped around more.

Also, why don’t you take over iTerm development since it has seemed to have died… :)

28 March 2006

by Rohsambo

This so made my day. Thank you.

Apple’s Cocoa text system is a complicated beast, but also extremely flexible, and with a bit of work, it can be molded to match many working styles. This how-to covers the 2 major ways of customizing the text input system: Default key bindings, and for still more control, input managers.


08 June 2006

by Miguel Cordova

Del key (backward delete key) has that symbol: ⌦ (x inside a box with an angle side facing to right) in my keyboard and could be mapped as hex code 4 (same as ^D) in order to delete from right to cursor

29 August 2006

by Johannes

Really cool! Search some hours till found this which works!!! Anyone know how to scroll repsectively move cursor page-up and down in vi or less ?

29 August 2006

by Johannes

oh i figured out… ^f and ^b or hex 6 and hex2 !

What about that option-delete Cocoa functionality for iTerm? Anyone know what bindings to change/enable for this? It would be handy for sure.

15 June 2007

by Angel Pidor

A very useful trick. Thanks!

You can do this via bindkey in ZSH by doing the following:

# use vi mode
bindkey -v

# go back a word with option arrow (staying in insert mode)
bindkey -M viins '^[[D' vi-backward-word
# go forward a word with option arrow (staying in insert mode)
bindkey -M viins '^[[C' vi-forward-word

You can also map Home and End to do what they say:

# use home and end to go to beginning and end of the line
bindkey -M viins '^[[H' vi-beginning-of-line
bindkey -M viins '^[[F' vi-end-of-line

And actual forward delete using the forward delete key:

# use delete as forward delete
bindkey -M viins '^[[3~' vi-delete-char

oops. nevermind that didn’t work. I was tricking myself into believing that it would. Now my arrow keys go word by word :)

03 November 2007

by Anonymous

The images are gone.

@ylon - After much struggle, I got option+delete working in iTerm to kill the word left of the cursor. Here is the link:

hmm, link got stripped out. I will try making my name link to the instructions for getting option + delete working.

great tip, thanks!

09 June 2008

by keitheis

claytron: viins works! Thanks!!