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

2 Answers 2

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 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
    Oct 7, 2017 at 6:49
  • Good points. I read your answer too quickly, 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. Jan 28, 2022 at 0:12
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.