tags:

views:

9428

answers:

7

Is there any way in the OS X Terminal to move the cursor word by word?

I knwo the combination CTRL+A to jump to the beginning of the current command, and CTRL+E to jump to the end.

But is there any way to jump word by word, like ALT+Left/Right in Cocoa applications does?

Thanks in advance,

Arne

A: 

Although I cannot answer directly, I know these shortcuts come from Emacs; you might look at its info file to see whether you can find the shortcuts you need. man bash can also be an option.

millenomi
+24  A: 

You should follow the instructions in this mactips post: http://www.mactips.org/archives/2007/12/20/word-movement-in-terminal/.

Works perfectly for me.

--- Edit

The original link appears to be broken, so here is another link to the same data.

Fil
Absolutely essential. I love it.
Jonathan
I've been wanting this for a while. Thanks.
Andy
Can't open, think the link is broken :( ?
Tobin Harris
+17  A: 

I have Alt-Left/Right working: open Preferences » Settings » Keyboard, set the entry for option cursor left to send string to shell: \033b, and set option cursor right to send string to shell: \033f. You can also use this for other Control key combinations.

Peter Hilton
To get the \033b, you actually need to press Esc, then b.
Matthew Schinckel
I'm on Snow Leopard and find that it only works when you enable `Enable option as meta key`. By the way, it's very cool. The key is sure to be much more convenient to be reached compare to Esc+B or Esc+F
Phương Nguyễn
works fine for me with enable option as meta key" off. ftr.
marc hoffman
+8  A: 

Out of the box you can use the quite bizarre Esc-F to move to the beginning of the next word and Esc-B to move to the beginning of the current word.

Kristian J.
It was inconvenient to re-press both keys in order to re-execute the command again. I find it pretty lame to do this in Mac OSX Terminal, compare to the GNOME Terminal.
Phương Nguyễn
+4  A: 

In Bash, these are bound to ESC-b and ESC-f. Bash has many, many more keyboard shortcuts; have a look at the output of bind -p to see what they are.

Andy Lynch
+4  A: 

If you check Use option as meta key in the keyboard tab of the preferences, then the default emacs style commands for forward- and backward-word and ⌥F (alt+f) and ⌥B (alt+b) respectively.

I'd recommend reading From Bash to Z-Shell. If you want to increase your bash/zsh prowess!

Matt
Yes, that works. But problem is when I want to type braces ([]{}|) which are on Option-7, Option-8 and Option-9. Options now is meta so it ends up with Meta-7, Meta-8 ... Any ideas?
Martin Wickman
+7  A: 

Actually it depends on what shell you use, however must shells have similar bindings. The bindings you are referring to (e.g. CTRL+A and CTRL+E) are bindings you will find in many other programs and they are used for ages, BTW also work in most UI apps.

Here's a look of default bindings for Bash:

Most Important Bash Keyboard Shortcuts

Please also note that you can customize them. You need to create a file, name as you wish, I named mine .bash_key_bindings and put it into my home directory. There you can set some general bash options and you can also set key bindings. To make sure they are applied, you need to modify a file named ".bashrc" that bash reads in upon start-up (you must create it, if it does not exist) and make the following call there:

bind -f ~/.bash_key_bindings

~ means home directory in bash, as stated above, you can name the file as you like and also place it where you like as long as you feed the right path+name to bind.

Let me show you some excerpts of my .bash_key_bindings file:

set meta-flag on
set input-meta on
set output-meta on
set convert-meta off
set show-all-if-ambiguous on
set bell-style none
set print-completions-horizontally off

These just set a couple of options (e.g. disable the bell; this can be all looked up on the bash webpage).

"A": self-insert
"B": self-insert
"C": self-insert
"D": self-insert
"E": self-insert
"F": self-insert
"G": self-insert
"H": self-insert
"I": self-insert
"J": self-insert

These make sure that the characters alone just do nothing but making sure the character is "typed" (they insert themselves on the shell).

"\C-dW": kill-word
"\C-dL": kill-line
"\C-dw": backward-kill-word
"\C-dl": backward-kill-line
"\C-da": kill-line

This is quite interesting. If I hit CTRL+d alone (I selected d for delete), nothing happens. But if I then type a lower case w, the word to the left of the cursor is deleted. If I type an upper case, however, the word to the right of the cursor is killed. Same goes for l and L regarding the whole line starting from the cursor. If I type an "a", the whole line is actually deleted (everything before and after the cursor).

I placed jumping one word forward on CTRL+f and one word backward on CTRL+b

"\C-f": forward-word
"\C-b": backward-word

As you can see, you can make a shortcut, that leads to an action immediately, or you can make one, that just inits a character sequence and then you have to type one (or more) characters to cause an action to take place as shown in the example further above.

So if you are not happy with the default bindings, feel free to customize them as you like. Here's a link to the bash manual for more information.

Mecki