Move Cursor Word by Word in Terminal for Mac OS X
If you spend a lot of time at the command line, you undoubtedly find yourself adjusting and editing text and commands, and you likely often need to move the cursor to a further position in the Terminal from where it’s actively located. Sure you can use the arrow keys to move left and right on a per-character basis, or you can use the handy put cursor at mouse position trick, but another option is to move the cursor position word by word in Terminal, skipping back or forward by entire word blocks rather than individual text characters.
There are a few ways to achieve this, but the easiest which requires no modifications to the Terminal uses a longstanding series of two different keyboard shortcuts:
Move Cursor Forward by Word in Terminal: Escape + F
Escape F moves the cursor forward a word at the command line.
Move Cursor Backward by Word in Terminal: Escape + B
Escape B moves the cursor backward by a word at the command line.
Moving forward and back word by word at the command line with these two keystrokes is demonstrated in the simple animated GIF below:
These two keystrokes have been around at the command line for ages, and so though they certainly work to navigate by word block in the Mac OS X Terminal, they should also work in just about any other unix based terminal you come across as well.
There are also two Mac OS specific keystrokes to navigate in text word by word forward and backward in Mac OS X Terminal and in most other Mac apps too:
Option + Left Arrow Moves Cursor Left by a Word in Mac OS X Terminal
Option / ALT and the Left Arrow will also move the cursor position left by a word throughout Mac OS.
Option + Right Arrow Moves Cursor Right by a Word in Mac Terminal
Option / ALT and the Right Arrow will send the cursor position right by a word throughout Mac OS as well.
Remember, the option key is the ALT key on Macs, and vice versa, though some models and regions will label them differently they are always the same key.
You shouldn’t need to make any adjustments to terminal for the option tricks to work either, but if you find they are not working in Terminal app you may have better results with enabling Option as Meta key in Terminal for Mac.
Actually there is a much better approach. Hold option and press the arrow keys left or right to move by word. Simple as that.
Or you add the line:
set -o vi
to your .profile and enjoy Vi Keybindings instead of Emacs in the Shell!
You can see an animated Gif using it here:
http://nop.wibben.de/vi-style-line-editing-in-der-shell/
cheers,
Björn
These are strictly speaking not features of Terminal but instead the emacs command line editing mode in the bash shell, which is the default shell in OS X.
There’s an excellent article here:
http://jblevins.org/log/kbd
on default OS X keybindings. There’s a further article at:
https://gist.github.com/cheapRoc/9670905
on how to use:
~/Library/KeyBindings/DefaultKeyBinding.dict
to set up all the “right” bindings :)
You can map forward delete word or character, backward delete word, move forward/backward word/character, etc. to keypress combos.
I’m always hamstrung when I try to do something on someone’s machine who hasn’t set up all the ‘necessary’ keybinding.
Nice and helpful. Now how do I get a real backspace and forward delete button and end button?
Hi,
what about iTerm?
Escape + B works on iTerm too