7

In bash line editing (or in any program that uses GNU readline), Meta-f moves to the right by one "word" and Meta-b move to the left by one "word", where a "word" is composed of letters and digits. These are the forward-word and backward-word commands. (Meta-f can be either the Alt modifier key or a prefix Escape key.)

For example, if I've typed

cat /etc/motd

then repeatedly typing Meta-b moves the cursor to the m, then the e, then the c.

Is there a command that moves similarly, but by whitespace-delimited words, so /etc/motd is a single word?

(I often work with very long file paths, and I'd like to be able to skip over them easily. My workaround is either to type the existing word-move commands repeatedly, or to type ^X ^E to launch an editor.)

In vim, w and b move forward and back by words, and W and B move by whitespace-delimited words. I'm looking for something similar.

I've looked through the readline documentation, and I suspect the answer is no, but perhaps I've missed something.

1

3 Answers 3

3

If you do set -o vi then readline takes vim style commands and w/W and b/B will have the same word/WORD behavior as in Vim itself. I routinely use W/B to skip over full paths, urls, etc. when editing the command line. I see from the output of bind -l that the readline commands bound to W and B are vi-forward-bigword and vi-backward-bigword.

Alternatively, we have shell-forward-word and shell-backward-word. These commands, per the bash man page, move by shell metacharacters which are characters that, when unquoted, separate words. Thus, they are a bit less "aggressive" than the vi-*-bigword commands but for jumping over long paths they'll almost always do the trick.

So, for example, you could do bind '"\C-x": shell-backward-word' and Ctrl+x will behave as described.

4
  • Try it on "foo bar" or $(echo foo bar) though. It's more like motions based on shell tokens Commented Oct 7, 2017 at 6:29
  • Yes, I said "when unquoted" which would not apply to "foo bar", for example. OP specifically mentioned long paths so I thought the shell-*-word functions would be of interest despite the "shell metacharacter" aspect if for some reason the vim-*-word functions were a problem.
    – B Layer
    Commented Oct 7, 2017 at 6:49
  • Good points. I read your answer too quickly, Commented Oct 7, 2017 at 6:59
  • 1
    Coming back to this several years later. I prefer to use emacs style with readline (ironic, since I use vim as my main editor). I've added these lines to my $HOME/.inputrc: "\eB": shell-backward-word and "\eF": shell-forward-word. With this, Escape-b moves back one alphanumeric word and Escape-B moves back one whitespace-delimited word; likewise for Escape-f and Escape-F. Commented Jan 28, 2022 at 0:12
3

Ctrl+Alt+f and Ctrl+Alt+b seem to be what you are looking for (probably Ctrl+Meta+f and Ctrl+Meta+b for you).

I came here looking for this, and I took hint from your Meta+f and Meta+b and tried combining with Ctrl, and it seemed to work for me!

I am on Ubuntu on WSL. Not sure if that matters.

1
  • That works, thanks. I'm still using my custom key bindings "\eB" for shell-backward-word and "\eF" for shell-forward-word. I find it a bit easier to type Esc Shift-B than Esc Ctrl-B. Commented Jun 7, 2023 at 20:07
2

You can add this:

set keymap vi-move
"\e[EMACS~": emacs-editing-mode
set keymap emacs
"\e[VI~": vi-movement-mode
"\ed": "\e[VI~dW\e[EMACS~"
"\ef": vi-fWord
"\eb": vi-bWord

To your inputrc or use zsh where that's more easily customizable. See this answer to another Q&A for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .