240

I just recently switched from bash to zsh, however I miss my Alt+LeftArrowKey and Alt+RightArrowKey to go back and forth a word at a time.

Right now, if I press Alt+LeftArrowKey I go back a couple of letters and then I'm stuck. I won't go any further backwards and it won't back to the end of the line with Alt+RightArrowKey as I would expect. I can't even use the arrow keys to go to the end of the line, only to the second to last. Can't input new chars on the line either or indeed delete.

How do I get my beloved shortcut back?

I'm on Mac OS X using Terminal if that's important.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Mads Ohm Larsen
  • 3,315
  • 3
  • 20
  • 22

13 Answers13

441

Run cat then press keys to see the codes your shortcut send.
(Press Ctrl+C to kill the cat when you're done.)
For me, (ubuntu, konsole, xterm) pressing Alt+ sends ^[[1;3D, so i would put in my .zshrc

bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word

(Actually I prefer to use Ctrl + arrow to move word by word, like in a normal textbox under windows or linux gui.)

Related question: Fix key settings (Home/End/Insert/Delete) in .zshrc when running Zsh in Terminator Terminal Emulator

Stphane
  • 3,368
  • 5
  • 32
  • 47
lolesque
  • 10,693
  • 5
  • 42
  • 42
  • 25
    You are my savior! On my Mac "Alt + <-" gave "^[b" and -> gave "^[f", so I added these. Works like a charm. Ctrl+arrows are reserved for switching between desktops on Mac. – Mads Ohm Larsen Sep 13 '12 at 12:31
  • 1
    One note is that for ctrl+<- cat was reporting `^[[D` for me while ctrl+v was reporting `^[OD`. Ctrl+v's suggestion worked, but cat's did not. Furthermore, ctrl+v was reporting the same thing for both alt+<- and alt+->. I was able to add these two by looking at cat's output and replacing any "[[" with "O" – Mike S Feb 06 '15 at 23:00
  • 14
    my output under cat is `^[^[[C` for forward word and `^[^[[D` for backward word, but updating for these in .zshrc didn't work for some reason. – Yunti May 19 '16 at 13:13
  • 8
    @Yunti - Putting `bindkey "^[^[[D" backward-word` and `bindkey "^[^[[C" forward-word` in *~/.zprofile* worked for me! – Fernando Espinosa Nov 06 '18 at 17:46
  • 4
    Don't forget to run `source ~/.zshrc` after editing your file – Aaron Aug 04 '20 at 18:15
  • thanks for your answer. been bugging for a while :) – ALFmachine Feb 17 '21 at 22:03
  • The key bindings will work with any shell, including Mac Terminal, iTerm, etc. – Anthony Avila Oct 16 '22 at 23:10
  • "Run `cat` then press keys to see the codes your shortcut send." best "btw" tip I've seen on SO – casraf Apr 29 '23 at 08:51
309

For anyone using iTerm, regardless of shell

All of the solutions offered here take a backwards approach in my opinion. You're essentially telling your shell to listen for some esc sequence or other key binding you have set in your terminal, creating compatibility issues when you switch shells (If you SSH into some other shell, switch from BASH to ZSH, etc and you lose some if not all of your keybindings).

Most shells have a set of default sequences that come pre-bound. Furthermore, while they aren't 100% consistent, they're close enough. So the easiest way that I have found to create keybinding for a particular action in the shell is to tell your terminal application to bind to the default keybindings that are consistent across shells.

I wrote a compressive solution for getting your terminal to respond as close to native mac keybindings..

enter image description here

Open the iTerm preferences +, and navigate to the Profiles tab (the Keys tab can be used, but adding keybinding to your profile allows you to save your profile and sync it to multiple computers) and keys sub-tab, click Key Mappings and enter the following:

Move cursor one word left

+ Send Hex Codes: 0x1b 0x62

Move cursor one word right

+ Send Hex Codes: 0x1b 0x66

And that should give you the desired behavior not just in ZSH, but also if you SSH into a server running BASH, irb/pry, node etc.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Travis
  • 13,311
  • 4
  • 26
  • 40
  • 2
    I was having trouble with the accepted solution because I had zsh working fine but not irb and other consoles. Erasing previous settings (in iTerm profile as suggested) works like a charm! – llekn May 28 '18 at 19:21
  • 2
    bindkey solution would make annoying error sound on Mac. This should be the right way to do it. – Weishi Z Jun 30 '18 at 06:42
  • 1
    @Lev Likely because either your shell isn't mapped to the default configuration. Run `bindkey | grep forward-word` you should see `"^[f" forward-word` returned but if you don't, you have 2 options. map to one of the others returned, or add `"^[f" forward-word` to your `.zshrc`. – Travis Oct 26 '18 at 17:57
  • I will note, that `"^[f"` seems to be the most consistently used binding for `forward-word` used across shells, and the only one set by default in my version of bash. – Travis Oct 26 '18 at 17:59
  • Somehow it worked with command button for me instead of option. Thanks man. :) – Vaibhav Jain Jun 29 '19 at 13:38
  • Any reason you recommend to use "Send Hex Codes", rather than "Send Escape Sequence" to send `^[b` and `^[f`, respectively? In iTerm2 I use the latter without an issue. – Nickolay Aug 05 '20 at 14:49
  • And those wondering why that's not the default: https://gitlab.com/gnachman/iterm2/-/issues/5451 – Nickolay Aug 05 '20 at 15:24
  • This is the correct solution which worked first time for me on a Mac - thank you! – Ermiya Eskandary Apr 29 '22 at 09:33
  • On the "key mappings" tab you can also load the "Natural Text Editing" preset. This will additionally give you things like alt+backspace for removing a word. – tjwelde Nov 17 '22 at 16:07
  • I selected natural text profile, as described below (https://stackoverflow.com/a/70451722/4936193) and it worked like a charm – jurl Jan 11 '23 at 12:40
  • The funny thing is that I just added the key to move to the left, which also works for the right. – Erick Jul 10 '23 at 01:01
92

Adding the following to ~/.zshrc worked for me on OSX Mountain Lion.

bindkey -e
bindkey '[C' forward-word
bindkey '[D' backward-word
Rob Sawyer
  • 2,163
  • 1
  • 24
  • 25
52

On MacOS High Siera 10.13.6 or Mojave 10.14.2 and using iTerm2 with ZSH To move from words I have to put like this:

bindkey "\e\e[D" backward-word
bindkey "\e\e[C" forward-word

Another solutions doesn't work fo rme

AllanLRH
  • 1,124
  • 1
  • 12
  • 23
Vetalll
  • 3,472
  • 6
  • 24
  • 34
35

For iTerm, go to where this screenshot shows and select "Natural Text Editing"

enter image description here

if you already had some key mappings it will ask below, select accordingly not to lose any special bindings you set before. however, if you don't remember adding any bindings or just started using iTerm (on this machine), you will be safe to choose "Remove"

enter image description here

mcvkr
  • 3,209
  • 6
  • 38
  • 63
9

Though not strictly answering your question, the default binding for forward-word and backward-word are alt-f resp. alt-b.

This works everywhere, does not require you to leave the home row, and has a nice mnemonic property (f=forward, b=back), while also being consistent with ctrl-f and ctrl-b being forward-character and backward-character.

Rip out your arrow keys!

0x89
  • 2,940
  • 2
  • 31
  • 30
4

To make it work for me I used this answer, however I had to swap the codes (left <-> right)

⌥+← Send Hex Codes: 0x1b 0x66
⌥+→ Send Hex Codes: 0x1b 0x62

and add the following to my ~/.zshrc

bindkey -e
bindkey "^[b" backward-word
bindkey '^[f' forward-word
evandrix
  • 6,041
  • 4
  • 27
  • 38
iRonin
  • 470
  • 4
  • 12
  • It would arguably be less confusing if you changed your `bindkey`s instead, to bind `^[f` to `forward-word`, and not to `backward-word`. – Nickolay Aug 05 '20 at 14:45
3

These keybindings work with Alacritty on Arch Linux, just add them to the ~/.zshrc file

bindkey -e

bindkey "^[[3~" delete-char                     # Key Del
bindkey "^[[5~" beginning-of-buffer-or-history  # Key Page Up
bindkey "^[[6~" end-of-buffer-or-history        # Key Page Down
bindkey "^[[H" beginning-of-line                # Key Home
bindkey "^[[F" end-of-line                      # Key End
bindkey "^[[1;3C" forward-word                  # Key Alt + Right
bindkey "^[[1;3D" backward-word                 # Key Alt + Left
alecodev
  • 31
  • 1
  • 3
2

On MacOS Monterey, use the following in ~/.zshrc to make SHIFT + Arrows jump words:

bindkey "^[[1;2C" forward-word
bindkey "^[[1;2D" backward-word

And this for Option + Arrows:

bindkey "^[^[[C" forward-word
bindkey "^[^[[D" backward-word
dacx
  • 824
  • 1
  • 9
  • 18
1

On Mavericks (10.9.4) the code is 1;5... so for binding alt with arrows I have my .zshrc using this:

bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word

You can use CTRL+V and then the command you want to use

in Yosemite use Rob's solution

ZeoS
  • 72
  • 1
  • 5
1

In zsh, you can use the bindkey command to see keyboard shortcuts.

Use bindkey to explore options that are available without custom keybindings.

Namely ^[b to move backward a word and ^[f to move forward a word.

MCI
  • 888
  • 1
  • 7
  • 13
0

If you're using iTerm in CSI u mode, the bindings for your .zshrc end up being:

bindkey '^[[1;3D' backward-word
bindkey '^[[1;3C' forward-word
David Lord
  • 628
  • 10
  • 29
0

If you want iTerminal to respect Emacs style shortcuts like ^Mf and ^Mb for forward/back a word I found best way to use this tip:

Making iTerm to translate 'meta-key' in the same way as in other OSes

HaveAGuess
  • 1,231
  • 1
  • 13
  • 23