Moving a word forward in z shell
Asked Answered
A

7

25

In zshell how to move forward word, I can set -o vi and use vi format. Is there a way to move forward in zshell by a word

Adenoma answered 6/2, 2010 at 5:41 Comment(0)
M
21

The ZLE widget for moving forward by one word is forward-word. Therefore, you may use bindkey to bind this widget to any key you want.

For example,

$> bindkey ^O forward-word

would allow you to move forward by one word when pressing Ctrl-O. Note that ^O is actually a quoted insert of Control followed by O.

Minda answered 6/2, 2010 at 6:37 Comment(0)
S
29

If you're using iTerm2 on OSX, you can use the Natural Text Editing preset under Preferences -> Profile -> Keys.

iTerm Keys Preferences

This supports these key combos (which are very similar to other editors):

  • alt/option + left = move left one word
  • alt/option + right = move right one word
Stob answered 12/10, 2017 at 21:49 Comment(5)
Note that you can find this preset only in the profile preferences, not in the global preferences.Lazo
This is literally the only thing that worked for me. Thanks Clay!Contractor
I am not getting the option of Natural Text Editing presetsRepression
This correctly made option-left move one word left, but option-right rings the terminal bell. Hmmm...Francophobe
And it also has broken 'delete', which now tries to do a completion of some sort, unless the command line is empty, in which case it closes the terminal (!). I wonder WTF I did?Francophobe
M
21

The ZLE widget for moving forward by one word is forward-word. Therefore, you may use bindkey to bind this widget to any key you want.

For example,

$> bindkey ^O forward-word

would allow you to move forward by one word when pressing Ctrl-O. Note that ^O is actually a quoted insert of Control followed by O.

Minda answered 6/2, 2010 at 6:37 Comment(0)
S
20

Your zsh command prompt works either like emacs or like vi. If it works like vi, put it in command mode (esc) and type w. If it works like emacs, use M-f.

More information available at man zshzle.

Sanasanabria answered 6/2, 2010 at 5:45 Comment(3)
For this to work you will need to enable vim mode 'bindkey -v' or if you are using oh-my-zsh install vi-mode pluginKerekes
@Kerekes the answer covers both vi-mode w and emacs mode M-f.Camper
This solution does not allow you to hold down of M and press f or b repeatedly to move multiple words at a time, which to me is one of the main points of wanting to skip by words.Carloscarlota
U
11

Similar to other answers, but for Zsh on iTerm it took me a while to find this:

If you are using Zsh, like Oh My Zsh, in iTerm then go to: Preferences > Profiles > Keys sub-menu

Click + sign

Add your shortcut combo, choose "Send Escape Sequence"

inputs for left and right below.

left:

[1;5D

right:

[1;5C
Undertaking answered 8/12, 2016 at 1:5 Comment(2)
Using Zsh in iTerm2, this does not work for me. After doing this, pressing the keys has no visible effect.Francophobe
When I define an escape sequence as you say, e.g. "[1;5D", the iTerm2 prefs dialog displays it as "^[ [1;5D". Should I actually be defining it as "1;5D", so that it displays as "^[ 1;5D"? Neither works for me though.Francophobe
H
6
  • forward word [Meta]+[f]
  • backward word [Meta]+[b]

On macOS, [option]+[→] and [option]+[←] work too.

Honduras answered 5/7, 2017 at 13:43 Comment(0)
C
1

In my zsh terminal it was already set to the below using bindkey

"^[f" forward-word 
"^[b" backward-word

I was not sure how to use this, read on Apple Forum's, this is basically

^+[ release it then press f or press esc release it then press f

Similarly for backward,

^+[ release it then press b or press esc release it then press b

Cloutier answered 13/1, 2022 at 22:20 Comment(0)
S
1

For me, nothing was working until I found out that having $EDITOR=vim makes your zsh start in vi mode automatically, which replaces these keybindings. The solution for me was to change my $EDITOR variable.

See: https://unix.stackexchange.com/questions/197839/why-does-exporting-vim-as-editor-in-zsh-disable-keyboard-shortcuts

Susann answered 29/9, 2023 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.