913

I know 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+/ in Cocoa applications does?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 2
    What shell are you using? Bash? – Sargun Dhillon Sep 17 '08 at 08:58
  • 4
    iTerm2 is so much nicer than the built-in terminal. I posted instructions below on how to customize the keyboard :) – cwd Nov 24 '11 at 00:40
  • 2
    why is this not move to superuser? –  Apr 01 '12 at 08:40
  • @user494461 because this is related to Terminal, an exclusively MacOS application. – Petruza May 23 '12 at 18:25
  • 26
    The `ALT` shortcuts have been added to Terminal in the latest OS X version. – Mosty Mostacho Oct 08 '13 at 20:58
  • 7
    This is not off topic in my opinion, Bash shell is a relevant programming environment, and readline bindings are pervasive in most popular Linux CLI apps. Also, look at the number of upvotes, the community has spoken. – GL2014 Jul 28 '14 at 13:11
  • 13
    Reopen vote, OS X *Terminal* very much counts as a programming tool. – djechlin May 27 '16 at 22:34
  • @djechlin, I couldn't disagree more strongly. Stack Overflow's scope is limited to tools _exclusively used by_ software developers. The terminal is for everyone -- developers, sure, but also sysadmins and just regular-Joe power users. – Charles Duffy May 14 '23 at 03:10

20 Answers20

661

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.

dcharles
  • 4,822
  • 2
  • 32
  • 29
Kristian J.
  • 10,273
  • 2
  • 21
  • 21
  • 60
    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 Jun 01 '10 at 02:35
  • 277
    You can enable "Use Option as meta key" in **Terminal > Preferences > Settings > [profile] > Keyboard**. Then you don't have to manually type the Esc separately. – Chris Page Oct 12 '11 at 05:13
  • 12
    Also if you hit `Esc`+`F` when at the end of a line or the other key combo at the start of the line, it just inserts the letter instead. Annoying! – Noldorin Jul 13 '12 at 01:30
  • 1
    @Noldorin at least in my mac, not anymore. Apparently, that got fixed somewhere along the way. – Olli Oct 16 '13 at 15:02
  • @Olli, ah yes, good call, looks like it got fixed... shame one still has to release and hit the Esc key again each time though! – Noldorin Oct 16 '13 at 17:51
  • @Noldorin: This is how terminals work. Enable “Option as Meta” to use the Option modifier key to send the ESC. Then you don’t need to press and release the ESC key. – Chris Page Mar 11 '14 at 06:09
  • 15
    Re “bizarre”: The “F” and “B” stand for “Forward” and “Back”. Control-F moves the cursor Forward one character, and Control-B moves the cursor back one. Using the Meta modifier (or ESC) means “move by words”. These are emacs-compatible bindings. If you’re using bash, there’s an option to use vi-compatible bindings, or you can customize them entirely with `~/.inputrc`. – Chris Page Mar 11 '14 at 06:11
  • 2
    @ChrisPage You very much rock my friend, very much. Thanks. – GL2014 Jul 28 '14 at 13:07
  • Use option as meta key works, just fyi the esc key seems not to work – User Oct 13 '14 at 17:48
  • 8
    Using option as meta is unfortunately not an option for those of us who need the option key for typing important shell symbols like ~ and | (like on a French keyboard). – Eric O. Lebigot Sep 13 '15 at 20:58
  • https://github.com/0nn0/terminal-mac-cheatsheet/wiki/Terminal-Cheatsheet-for-Mac-(-basics-) –  Apr 22 '16 at 01:06
  • iTerm users: https://codingphilosophy.wordpress.com/2013/04/20/move-the-cursor-word-by-word-on-mac-os-x-iterm2/ – Dave Thomas Nov 08 '16 at 22:50
  • 1
    In iTerm2, you can enable "Use alt as meta key" in Perferences > Profiles > Keyboard Behavior > Left option key acts as +Esc, Then you don't have to manually type the Esc separately. – 水清木华 Feb 03 '17 at 09:43
  • Why does it consider underscore as a word boundary? Is it possible to change that behavior? – Atif May 22 '17 at 14:41
  • 1
    @ChrisPage, Since when is `Esc` a meta key? Doesn't it mean "Cancel"? – Pacerier Nov 01 '17 at 19:47
  • @Pacerier In normal GUI use it does...but not in many terminal apps! These bindings are based on how Emacs does things. I've heard that Emacs was designed for a different keyboard than the one we have today, one that had more modifiers. Nowadays we use Esc, X (Option-X actually just emulates Esc, X) instead of the nonexistent Meta-X. – SilverWolf May 27 '19 at 21:30
  • @Pacerier Also, a lot of special keys, like the arrows and F-keys, actually send _sequences_ starting with Esc. This makes use of Esc by itself a little difficult–you have to wait to see whether it's Esc and then some more typing, or the terminal sending you an escape sequence. There's literally _no way to tell!_ – SilverWolf May 27 '19 at 21:30
  • This is so bizarre, especially the MacBook series with Touch Bar.. no physical Esc button – Ardiya May 29 '20 at 05:57
554

On macOS (all versions) the following keyboard shortcuts work by default.

  • ALT+F to jump Forward by a word.
  • ALT+B to jump Backward by a word.

Note that you have to make set the Option key to act like the Meta key. You can do this in Terminal by accessing preferences (CMD+,) and selecting Profiles -> Keyboard. In iTerm2 select Profiles -> Keys -> General and select "Option key as Esc+."

Terminal and iTerm2 Preference Screens

Additionally some Emacs-style key bindings for simple text navigation seem to work on bash shells. You can use:

  • CTRL+F to move forward by a char
  • CTRL+B to move backward by a char
  • CTRL+A to jump to start of the line
  • CTRL+E to jump to end of the line
  • CTRL+K to kill the line starting from the cursor position
  • ALT+D to delete a word starting from the current cursor position
  • CTRL+W to remove the word backwards from cursor position
  • CTRL+Y to paste text from the kill buffer
  • CTRL+R to reverse search for commands you typed in the past from your history.
  • CTRL+S to forward search (works in ZSH for me but not bash)
CupawnTae
  • 14,192
  • 3
  • 29
  • 60
Varun Katta
  • 6,446
  • 1
  • 17
  • 5
  • 1
    Thanks! I was wondering why it didn't work out of the box, that is exactly what I needed. – Flov Aug 20 '11 at 15:56
  • 29
    You can change meta in iterm2 as following: Preferences -> Profiles -> Keys -> Left option key acts as `+ESC`, then you can use OPTION f/b to move word forward and backward accordingly. [More](http://ss64.com/bash/syntax-keyboard.html) – Alan Dong Jun 18 '14 at 14:31
  • 6
    [ __Ctrl + W__ ] - to remove the word backwards from cursor position – YemSalat Apr 20 '16 at 01:37
  • 1
    **[ ctrl + u ]** - to delete entire entry – Cornelius Jun 29 '18 at 11:39
  • 1
    This works, but when I got a new MacBook with Catalina on it, alt (⌥)+F and friends jump over `/` characters, which is a pain. Any idea how to include `/` as a word separator? – Ben Sep 11 '20 at 18:44
  • Hi, @Ben, I just posted a similar question regarding the underscore character. – Pat Aug 31 '23 at 11:57
308

Here's how you can do it

By default, the Terminal has these shortcuts to move (left and right) word-by-word:

  • esc+B (left)
  • esc+F (right)

You can configure alt+ and to generate those sequences for you:

  • Open Terminal preferences (cmd+,);
  • At Settings tab, select Keyboard and double-click ⌥ ← if it's there, or add it if it's not.
  • Set the modifier as desired, and type the shortcut key in the box: esc+B, generating the text \033b (you can't type this text manually).
  • Repeat for word-right (esc+F becomes \033f)

Alternatively, you can refer to this blog post over at textmate:

http://blog.macromates.com/2006/word-movement-in-terminal/

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Fil
  • 3,597
  • 2
  • 18
  • 8
  • 8
    For MacOSX 10+ and iTerm users: Either you assign the keys shortcut on the global preferences or as a profile key combination. Make sure you select : "Send Escape Sequence" on the dropdown select and input just `B` for _back_ or `F` for _forward_ into the `Esc+` field – Gus Jul 21 '15 at 03:13
  • +1 for a great mnemonic that I didn't think of earlier. I will definitely/unfortunately _not_ remember this now because I configured `alt left` and `alt right` to generate those sequences – Arc676 Oct 16 '15 at 12:28
  • If I add the Key Mapping to Terminal it binds it to '^[B' instead of Esc-B. – Andrea Bergonzo Sep 18 '17 at 11:59
192

Switch to iTerm2. It's free and much nicer than plain old terminal. Also it has a lot more options for customization, like keyboard shortcuts.

Also I love that you can use cmd and 1-9 to switch between tabs. Try it and you will never go back to regular terminal :)

How to set up custom keyboard preferences in iterm2

  • Install iTerm2
  • Launch and then go to preference pane.
  • Choose the keyboard profiles tab
  • You will either need to copy the profile to something new and then delete the arrow key shortcuts such as ^+ Right/Left or if you don't care about a backup just delete them from the default profile.
  • Next make sure your modified profile is selected (starred)

Picture 1.png

  • Now choose the keyboard tab (very top row)

iTerm 2

  • Click on the plus button to add a new keyboard shortcut
  • In the first box type CMD+Left arrow
  • In the second box choose "send escape code"
  • In the third box type the letter B

Picture 2.png

  • Repeat with desired key combinations. escape+B moves one word to the left, escape+f moves one word to the right.
  • you may also wish to set up cmd+d to delete the word in front of the cursor with escape+d

I often hit the wrong button (cmd / control / alt) with an arrow key and so i have my arrow key combinations with those buttons all set to jump forward and back words, but please do what fits you best.

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
cwd
  • 53,018
  • 53
  • 161
  • 198
  • 5
    No matter what I do, I cannot enter `Ctrl + Arrow Left` or `Right` - `Up` and `Down` works :/ tried with and without the new profile set as default – Tobi Oct 19 '17 at 11:41
  • 6
    Solution: I had to disable the mission control shortcuts under MacOS System Preferences -> Keyboard :) – Tobi Oct 19 '17 at 12:45
  • The above instruction doesn't work with iTerm2 any more. Please see my below answers for iTerm2 Build 3.3.4. – Hang Oct 01 '19 at 17:54
185

Actually there is a much better approach. Hold option ( alt on some keyboards) and press the arrow keys left or right to move by word. Simple as that.

option
option

Also ctrle will take you to the end of the line and ctrla will take you to the start.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
mdubez
  • 3,024
  • 1
  • 17
  • 10
  • 9
    Sometimes this doesn't work in certain cases. For me, when I opened Terminal.app, it was fine, but for IntelliJ, it just printed `[D` and `[C`. To fix this, add `bind '"[D": backward-word'` and `bind '"[C": forward-word'` to my `.bashrc`. – Jamie Counsell May 05 '17 at 15:44
  • @JamieCounsell I added bind thing to my `.bashrc` but it didn't work, anything wrong for me ? (I use RubyMine) – 张艳军 May 23 '17 at 05:48
  • @张艳军 I'm not sure if RubyMine reads `.bashrc` when the terminal boots. Try `echo "here"` and see if that message displays when you open a new terminal. Also, you have to close and re-open the terminal or run `source ~/.bashrc` to get it to read the new file. – Jamie Counsell May 23 '17 at 15:17
  • Worked perfectly for me on macOS Catalina 10.15.5, thanks – Adam Nov 18 '20 at 08:00
79

I have Alt+/ 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.

dcharles
  • 4,822
  • 2
  • 32
  • 29
Peter Hilton
  • 17,211
  • 6
  • 50
  • 75
  • 20
    To get the \033b, you actually need to press Esc, then b. – Matthew Schinckel Sep 17 '08 at 10:08
  • 7
    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 Jul 20 '10 at 06:31
  • 2
    Out of curisoity, what exactly is going on here? Why is this code different to begin with in OSX? How can I find other such codes? – Koobz Jun 29 '11 at 19:59
  • in my mac its Preferences > Profiles > Keys left option key set as +Ecs – Arun Aug 05 '15 at 09:24
  • This sounds great for what I want, but under preferences -> Settings -> Keyboard I don't see anywhere where I can set such entries. Needs more explanation? – Flion Jun 23 '22 at 07:58
50

Use Natural Text Editing preset!

enter image description here

Essentially it binds, among other key sequences, Option + LeftArrow to ^[b sequence and Option + RightArrow to ^[f

This works in fish and bash, as well as in psql terminal.

Dziamid
  • 11,225
  • 12
  • 69
  • 104
38

Actually it depends on what shell you use, however most 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
  • 125,244
  • 33
  • 244
  • 253
  • how is `command` key code? In your example, `control` is `\C`, but how is `command`? – Paschalis Nov 07 '14 at 16:00
  • Basically I want to map Ctrl+A to cmd+left (go to the beginning of the line) – Paschalis Nov 07 '14 at 16:14
  • 1
    @Paschalis On MacOS X, the command key maps to `Meta` in bash (`\M`), cmd+left would be `\M-\e[D`. However, you cannot map cmd+... to anything, all command shortcuts are handled (and thus swallowed) by the Terminal app itself (those never get forwarded to the shell) and cmd+left maps to "previous window" if you have multiple terminal windows open. Sorry, it's just not possible with Terminal app. – Mecki Nov 11 '14 at 09:42
37

Hold down the Option key and click where you'd like the cursor to move

Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
18

If you happen to be a Vim user, you could try bash's vim mode. Run this or put it in your ~/.bashrc file:

set -o vi

By default you're in insert mode; hit escape and you can move around just like you can in normal-mode Vim, so movement by word is w or b, and the usual movement keys also work.

jches
  • 4,542
  • 24
  • 36
15

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!

dcharles
  • 4,822
  • 2
  • 32
  • 29
Matt
  • 5,522
  • 5
  • 29
  • 24
  • 1
    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 Oct 06 '09 at 15:17
14

As of Mac OS X Lion 10.7, Terminal maps Option-Left/Right Arrow to Esc-b/f by default, so this is now built-in for bash and other programs that use these emacs-compatible keybindings.

Chris Page
  • 18,263
  • 4
  • 39
  • 47
8

Under iterm2's Preferences > Profile > Keys, you click the + below Key Mappings and record a new shortcut. For Action, select Send Escape Sequence and type b or f for backwards and forwards respectively.

When I tried to record one for (Ctrl+), I noticed in the Keyboard Shortcut field that the arrow never showed up. Turns out I had to disable the default mac's System Preferences > Keyboard > Shortcuts > Mission Control shorcuts first to get things to work, as they'll override iterm2's default shortcuts. Should be true for the standard terminal app, too.

Keyboard system preferences

qix
  • 7,228
  • 1
  • 55
  • 65
8

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.

dcharles
  • 4,822
  • 2
  • 32
  • 29
Andy Lynch
  • 1,228
  • 9
  • 15
4

For some reason, my terminal's option+arrow weren't working. To fix this on macOS 10.15.6, I opened the terminal app's preferences, and had to set the bindings.

Option-left = \033b
Option-right = \033e

Keyboard settings in Mac terminal app

For some reason, the option-right I had was set up to be \033f. Now that it's fixed, I can freely skip around words in the termianl again.

ubershmekel
  • 11,864
  • 10
  • 72
  • 89
3

Here's the CLI way to do so, verified it works on bash.

Add the following to your ~/.inputrc:

# macOS Option + Left/Right arrow keys to move the cursor wordwise
"\e\e[C": forward-word
"\e\e[D": backward-word

The advantage of this method is that it is terminal application agnostic - doesn't matter whether you use Terminal.app, iTerm2, or any other application.

Inspiration got from this other answer.

thiagowfx
  • 4,832
  • 6
  • 37
  • 51
  • +1 for referring to `~/.inputrc`, that solved a 3h issue I had with all shell with commands `Ctrl-A` or `Ctrl-E` not working simply because it had a `set editing-mode vi` line on it.............. – Niloct Oct 28 '22 at 20:39
1

As answered previously, you can add set -o vi in your ~/.bashrc to use vi/vim key bindings, or else you can add following part in .bashrc to move with Ctrl and arrow keys:

# bindings to move 1 word left/right with ctrl+left/right in terminal, just some apple stuff!
bind '"\e[5C": forward-word'
bind '"\e[5D": backward-word'
# bindings to move 1 word left/right with ctrl+left/right in iTerm2, just some apple stuff!
bind '"\e[1;5C": forward-word'
bind '"\e[1;5D": backward-word'

To start effect of these lines of code, either source ~/.bashrc or start a new terminal session.

1

New answer for iTerm2 Build 3.3.4 users:

Step 1: (macOS X) System Preferences > Keyboard > Shortcuts tab > Select Mission Control (left panel) > Uncheck shortcuts that labeled as "Move left a space" and "Move right a space"

Step 2: (iTerm2 Build 3.3.4) Preferences > Profiles > Select * Default (left panel) > Keys tab > Delete both "⌥->" and "⌥<-" entries > Set both "Left Option (⌥) Key:" and "Right Option (⌥) Key:" to Esc+

No messing around with shell profiles, no messing around with inferior masOS (default) Terminal, no awkwards Esc+F/B, rinse & repeat non-sense.

Done deal!!!

Enjoy this tip, my fellow PROGRAMMERS!

Hang
  • 956
  • 6
  • 12
0

in Cot Editor or in Terminal simple way to skip word without changing any settings is to use

Option_Key + -> (right arrow) to move one word to right

Option_key + <- (left arrow) to move one word to left

enter image description here

raja
  • 35
  • 2
-1

Just check the "Use Option as meta key" option in Terminal > Preferences > Settings > [profile] > Keyboard, as mentioned here already by @cris-page.

Note however, that in macOS Catalina (10.15) and newer, zsh becomes the default shell for newly added users: its default configuration considers only whitespaces as word-boundaries, whereas the old bash makes meta-left/right jump to the nearest non-alphanumerical character (similar to B/W as opposed to b/w for those familiar with vim):

                  v----v- bash jumps here
$ vim some-folder/what.txt_<- jump left twice from here
  ^---^- zsh jumps here by default

(similar motions are true for meta-backspace as well)

There are more than one ways to make zsh command line editor navigation work similarly to bash's - here is one such method:

# Place in your profile init script, e.g. `~/.zshrc`
autoload -U select-word-style
select-word-style bash

conny
  • 9,973
  • 6
  • 38
  • 47
  • IMO nobody "needs" iTerm, and for most users it only adds one extra layer of configuration-confusion. – conny Sep 09 '21 at 08:17