在vi模式下在bash中插入最后一个参数而不插入前导空格

mik*_*ike 7 bash readline vi-mode

在默认 (emacs) 模式下使用 bash 当我点击Esc,? ..

$ echo hello
hello
$ hello  # I hit `<ESC>.` to insert this
Run Code Online (Sandbox Code Playgroud)

请注意hello,当我点击时插入的单词前没有空格Esc,?..

如果我切换到 vi 模式并进行配置,.我会得到一个前导空格:

$ set -o vi
$ bind -m vi-command ".":yank-last-arg
$ echo hello
hello
$  hello  # I hit `<ESC>.` to insert this. Note the leading space.
Run Code Online (Sandbox Code Playgroud)

有没有办法配置 bash/readline 来避免这个前导空间?

Qua*_*odo 2

这确实看起来像一个错误,但实际上 Bash 只是试图遵循POSIX 指定的行为_

[count]_
在当前字符位置之后附加一个 <space>,然后在 <space> 之后附加上一个输入行中的最后一个 bigword。然后在刚刚附加的最后一个字符后进入插入模式。使用数字count ,在上一行中附加第count个大字。

作为解决方法,请将其添加到您的~/.inputrc. 如果需要,您可以将 更改\M-h为其他未绑定的密钥。

set editing-mode vi
set keymap vi-command
"\M-h":history-expand-line
".":"a!$\e\M-hA"
Run Code Online (Sandbox Code Playgroud)

现在,打开一个新终端。.在正常模式下敲击时,

  • !$被插入到命令行中。
  • \e(表示Esc)返回正常模式。
  • \M-h触发history-expand-line操作,该操作扩展$!到最后一个参数的值。
  • A移动到行尾并进入插入模式。
$ echo "X Y Z"
X Y Z
$ "X Y Z" #<ESC>. inserts this
Run Code Online (Sandbox Code Playgroud)
$ echo "X Y Z"
X Y Z
$ cat "X Y Z" #cat <ESC>. inserts this
Run Code Online (Sandbox Code Playgroud)