kal
kal

Reputation: 29441

Moving a word forward in z shell

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

Upvotes: 25

Views: 24407

Answers (7)

lesterfernandez
lesterfernandez

Reputation: 505

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

Upvotes: 1

Suave Bajaj
Suave Bajaj

Reputation: 109

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

Upvotes: 1

Clay
Clay

Reputation: 11635

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

Upvotes: 30

Aneil Mallavarapu
Aneil Mallavarapu

Reputation: 3525

  • forward word [Meta]+[f]
  • backward word [Meta]+[b]

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

Upvotes: 6

bdanin
bdanin

Reputation: 771

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

Upvotes: 11

sykloid
sykloid

Reputation: 101416

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.

Upvotes: 21

Carl Norum
Carl Norum

Reputation: 225272

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.

Upvotes: 20

Related Questions