Looking for ALT+LeftArrowKey solution in zsh
Asked Answered
I

14

261

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.

Infare answered 12/9, 2012 at 6:41 Comment(0)
B
465

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

Briannabrianne answered 13/9, 2012 at 10:1 Comment(8)
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.Infare
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"Abundance
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.Stereograph
@Stereograph - Putting bindkey "^[^[[D" backward-word and bindkey "^[^[[C" forward-word in ~/.zprofile worked for me!Outsail
Don't forget to run source ~/.zshrc after editing your fileProcrastinate
thanks for your answer. been bugging for a while :)Equine
The key bindings will work with any shell, including Mac Terminal, iTerm, etc.Assimilable
"Run cat then press keys to see the codes your shortcut send." best "btw" tip I've seen on SOWun
F
330

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.

Flanna answered 9/7, 2015 at 21:58 Comment(11)
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!Wineskin
bindkey solution would make annoying error sound on Mac. This should be the right way to do it.Toxinantitoxin
@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.Flanna
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.Flanna
Somehow it worked with command button for me instead of option. Thanks man. :)Balduin
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.Tericaterina
And those wondering why that's not the default: gitlab.com/gnachman/iterm2/-/issues/5451Tericaterina
This is the correct solution which worked first time for me on a Mac - thank you!Weese
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.Incontrollable
I selected natural text profile, as described below (https://mcmap.net/q/109162/-looking-for-alt-leftarrowkey-solution-in-zsh) and it worked like a charmSholom
The funny thing is that I just added the key to move to the left, which also works for the right. 🤣Doubletree
C
93

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

bindkey -e
bindkey '[C' forward-word
bindkey '[D' backward-word
Congruency answered 7/5, 2013 at 4:37 Comment(6)
This solution triggers a "bell" in iTerm 2 with every use, so jumping between words can get really annoying.Bourassa
anyone figure out how to disable this from happening^ @MatthewMorekWestley
'\e\e' prevents the bell. My configuration on El Capitan with iTerm 2 in Terminal compatibility mode is bindkey "\e\e[D" backward-word bindkey "\e\e[C" forward-wordColitis
Thank you, this solved the issue on macOs Sierra. I don't get any bell sounds on either Terminal or iTerm2.Diplomatics
How do you set it to move from the end of words and not the start or them?Worldshaking
Also works on hackintosh ventura with ISO keyboard :DArchivist
J
55

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

Jeffreyjeffreys answered 9/10, 2018 at 6:50 Comment(5)
This is the only solution that worked for me as well. Thank you! Mac OS MojavePhysician
Works on OS Catalina as well!Steinway
\e\e[D. what keys it representsUndersized
Many thanks, solved the issue for me with Big Sur.Interdenominational
Works, AND had no 'boop' sound <3Shoup
F
49

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

Floor answered 22/12, 2021 at 15:50 Comment(4)
How is this not the accepted answer, perfect!Darelldarelle
YES! I'm using iTerm and this worked, thanks!Sholom
This is the best working answer for iTerm!Guardrail
Perfect, just do this. No fancy files.Haven
N
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!

Nonna answered 10/3, 2016 at 15:49 Comment(1)
doesn't work for me in zsh/iterm2, prints ƒ and ∫ (c-b cf works).Socman
F
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
Foliolate answered 23/3, 2018 at 10:15 Comment(1)
It would arguably be less confusing if you changed your bindkeys instead, to bind ^[f to forward-word, and not to backward-word.Tericaterina
A
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
Adamek answered 5/1, 2022 at 16:31 Comment(0)
I
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
Ilysa answered 20/3, 2022 at 9:43 Comment(0)
A
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

Archivist answered 19/3, 2014 at 15:6 Comment(1)
bindkey "[C" forward-word bindkey "[D" backward-wordDashiell
H
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.

Hardnosed answered 20/6, 2019 at 19:23 Comment(0)
T
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
Tesch answered 30/8, 2020 at 22:56 Comment(0)
L
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

Leo answered 28/1, 2021 at 9:14 Comment(0)
A
0

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. 🤔

Andry answered 3/10, 2023 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.