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 ().

31 Comments

  1. 07 Jan 2006 | # Geir-Tore wrote…

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

  2. 07 Jan 2006 | # Geir-Tore wrote…

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

  3. 07 Jan 2006 | # Alain Ravet wrote…

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

  4. 07 Jan 2006 | # Alain Ravet wrote…

    oops. Sorry, take 2:

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

  5. 07 Jan 2006 | # Roland wrote…

    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.

  6. 07 Jan 2006 | # brian w. wrote…

    Thanks for this excellent tip. That made my day.

  7. 07 Jan 2006 | # Greg wrote…

    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.

  8. 07 Jan 2006 | # sjk wrote…

    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.

  9. 07 Jan 2006 | # Greg wrote…

    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!

  10. 07 Jan 2006 | # James A Baker wrote…

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

  11. 09 Jan 2006 | # Greg wrote…

    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.

  12. 10 Jan 2006 | # luke hartman wrote…

    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!

  13. 14 Jan 2006 | # Kumar McMillan wrote…

    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? :)

  14. 26 Jan 2006 | # Clayton Hynfield wrote…

    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

  15. 27 Jan 2006 | # Max Lein wrote…

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

  16. 14 Feb 2006 | # Nick Eff wrote…

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

  17. 21 Feb 2006 | # ylon wrote…

    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… :)

  18. 28 Mar 2006 | # Rohsambo wrote…

    This so made my day. Thank you.

  19. 06 Apr 2006 | # encro wrote…

    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.

    http://hcs.harvard.edu/~jrus/Site/Cocoa%20Text%20System.html

  20. 08 Jun 2006 | # Miguel Cordova wrote…

    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

  21. 29 Aug 2006 | # Johannes wrote…

    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 ?

  22. 29 Aug 2006 | # Johannes wrote…

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

  23. 19 Apr 2007 | # ylon wrote…

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

  24. 15 Jun 2007 | # Angel Pidor wrote…

    A very useful trick. Thanks!

  25. 24 Jul 2007 | # claytron wrote…

    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
    
  26. 26 Jul 2007 | # claytron wrote…

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

  27. 03 Nov 2007 | # Anonymous wrote…

    The images are gone.

  28. 28 Jan 2008 | # kortina wrote…

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

  29. 28 Jan 2008 | # kortina wrote…

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

  30. 06 Feb 2008 | # andreamatesi wrote…

    great tip, thanks!

  31. 09 Jun 2008 | # keitheis wrote…

    claytron: viins works! Thanks!!

Comments closed, you can use the mailing list for discussion.