Mads Ohm Larsen
Mads Ohm Larsen

Reputation: 3715

Looking for ALT+LeftArrowKey solution in zsh

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.

Upvotes: 277

Views: 102427

Answers (16)

lain0
lain0

Reputation: 109

Under MacOS Sequoia 15 i use xxd to determine keypressed codes for current keymode with terminal settings etc

for iterm2 in ~/.zshrc i need:

bindkey '^[[1;9C' forward-word
bindkey '^[[1;9D' backward-word

and for alacrtitty in default settings and for tmux i need

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

Upvotes: 1

mmk
mmk

Reputation: 1

my setup

  • M1 Macbook Pro, running 15.0.1
  • zsh terminal
  • goal: use Option + right/left arrow keys to skip by word
  • TLDR: put this in your ~/.zshrc:
# option + left/right arrow keys fix
bindkey "^[b" backward-word
bindkey "^[f" forward-word

steps taken to solve (in case you have slightly different goals or setup)

  1. find out what each of these key presses maps to: (a) Option + Right Arrow Key and (b) Option + Left Arrow Key
  • run cat in the terminal. then press Option + Right Arrow Key. check output. in my case, ^[f is the result.
  • then press Option + Left Arrow Key. check output. in my case, ^[b is the result.

  1. set the mapping in ~/.zshrc. the general formula is bindkey "<mapping>" [back/for]ward-word. So replace the <mapping> with your result from above and choose either backward-word or forward-word depending on what behavior you want for the given keymapping. i used the following:
# option + left/right arrow keys fix
bindkey "^[b" backward-word
bindkey "^[f" forward-word

  1. run source ~/.zshrc to apply and save changes

Upvotes: 0

conny
conny

Reputation: 10145

The most simple way is to go to Terminal -> Preferences -> Keyboard and toggle “Use Opt as Meta-key” ON.

It has been that way with Terminal.app for all shells (-that rely on libreadline, I presume) since the beginning of OS X / macOS.

Don’t ask me why Apple never made this the default. 🤔

Upvotes: 0

Travis
Travis

Reputation: 14436

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.

Upvotes: 348

mcvkr
mcvkr

Reputation: 3912

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

Upvotes: 54

dacx
dacx

Reputation: 844

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

Upvotes: 2

alecodev
alecodev

Reputation: 31

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

Upvotes: 3

iRonin
iRonin

Reputation: 470

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

Upvotes: 4

HaveAGuess
HaveAGuess

Reputation: 1241

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

Upvotes: 0

David Lord
David Lord

Reputation: 638

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

Upvotes: 0

MCI
MCI

Reputation: 922

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.

Upvotes: 1

Vetalll
Vetalll

Reputation: 3700

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

Upvotes: 56

lolesque
lolesque

Reputation: 11970

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

Upvotes: 496

0x89
0x89

Reputation: 2920

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!

Upvotes: 9

ZeoS
ZeoS

Reputation: 72

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

Upvotes: 1

Rob Sawyer
Rob Sawyer

Reputation: 2183

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

bindkey -e
bindkey '[C' forward-word
bindkey '[D' backward-word

Upvotes: 95

Related Questions