How do you move to the next or previous word in a Terminal in Mac OS? [duplicate]

I usually use Linux, no expert at all (very far from it) but I use it daily.

Sometimes I find myself using (someone else's) Mac and typing commands into a Terminal.

In Linux, I can use Ctrl+Left and Ctrl+Right to move backward or forward in the current line one word at a time (as in most text editors btw). If I try to do that in Mac OS, it switches workspaces, or brings to front another window, or some irritating thing like that. I think I tried both the Command and the Ctrl (proper) key.

Is there a different key combination for that in Mac OS? At the end I find myself holding down the arrow kay to move one character at a time, which in Mac OS for some reason is like 10 times slower than in Linux and Windows.


Solution 1:

Opt-B moves one word backwards, Opt-F one word forward. These are emacs keybindings your shell (bash) supports and has enabled by default. You need to have checked the Use option as meta key preference (pictured below).


Additionally, in your Terminal's profile settings, you can assign special escape sequences to e.g. Opt-Left and Opt-Right to make the familiar keyboard shortcuts work in Terminal, like these:

enter image description here

enter image description here

Use \033[5D to move left, and \033[5C to move right.

Solution 2:

If you are using iTerm, and want the Linux like Crtl + L / R mapping, slhck's suggestion works well.

Just add:

"\e[1;5D": backward-word
"\e[1;5C": forward-word

to ~/.inputrc (create the file, if it does not exist), for Ctrl - left, and Ctrl - right, respectively.