如何使用vi设置调用bash中的最后一个参数

Kos*_*sak 8 command-line bash keyboard-shortcuts vi

set -o vi在 bash 中使用设置。Alt+.此处的快捷方式(调用前一个命令的最后一个参数)在 emacs 模式下不起作用,那么 vi 的等效项是什么?

Mar*_*slo 11

有多种方法可以获取最后一个命令的最后一个参数:

1. inputrc: insert-last-argument & yank-last-arg

将以下代码复制到您的~/.inputrc文件中

set editing-mode vi
# Insert Mode
set keymap vi-insert
"\e.":yank-last-arg
"\e_": yank-last-arg
Run Code Online (Sandbox Code Playgroud)

您可以使用我的 inputrc 文件。这里是 inputrc 手册insert-last-argumentyank-last-arg

2. 单词指示符:!!:$ & !$

例如:

?? (marslo@MarsloJiao ~) ->
?? # echo arg1 arg2 arg3 arg4 arg5
arg1 arg2 arg3 arg4 arg5

?? (marslo@MarsloJiao ~) ->
?? # echo !$
echo arg5
arg5

?? (marslo@MarsloJiao ~) ->
?? # echo arg1 arg2 arg3 arg4 arg5
arg1 arg2 arg3 arg4 arg5

?? (marslo@MarsloJiao ~) ->
?? # echo !!:$
echo arg5
arg5

?? (marslo@MarsloJiao ~) ->
?? # echo arg1 arg2 arg3 arg4 arg5
arg1 arg2 arg3 arg4 arg5

?? (marslo@MarsloJiao ~) ->
?? # echo !!:^
echo arg1
arg1

?? (marslo@MarsloJiao ~) ->
?? # echo arg1 arg2 arg3 arg4 arg5
arg1 arg2 arg3 arg4 arg5

?? (marslo@MarsloJiao ~) ->
?? # echo !!:2-4
echo arg2 arg3 arg4
arg2 arg3 arg4
Run Code Online (Sandbox Code Playgroud)

Shell Word Designator的手册显示:

!!:$

designates the last argument of the preceding command. This may be shortened to !$.
Run Code Online (Sandbox Code Playgroud)

0(零)

The 0th word. For many applications, this is the command word.
Run Code Online (Sandbox Code Playgroud)

n

The nth word.
Run Code Online (Sandbox Code Playgroud)

^

The first argument; that is, word 1.
Run Code Online (Sandbox Code Playgroud)

$

The last argument.
Run Code Online (Sandbox Code Playgroud)

%

The word matched by the most recent ‘?string?’ search.
Run Code Online (Sandbox Code Playgroud)

xy

A range of words; ‘-y’ abbreviates ‘0-y’.
Run Code Online (Sandbox Code Playgroud)

*

所有单词,除了第 0 个。这是“1-$”的同义词。如果事件中只有一个词,则使用“ 不是错误;在这种情况下返回空字符串。X

Abbreviates ‘x-$’
Run Code Online (Sandbox Code Playgroud)

X-

Abbreviates ‘x-$’ like ‘x*’, but omits the last word.
Run Code Online (Sandbox Code Playgroud)

3.Shell特殊参数:$_

例如:

?? (marslo@MarsloJiao ~) ->
?? # echo very-very-very-very-very-long-argument
very-very-very-very-very-long-argument

?? (marslo@MarsloJiao ~) ->
?? # echo $_
very-very-very-very-very-long-argument

?? (marslo@MarsloJiao ~) ->
?? # ls /usr/local/etc/

?? (marslo@MarsloJiao ~) ->
?? # cd $_

?? (marslo@MarsloJiao /usr/local/etc) ->
?? #
Run Code Online (Sandbox Code Playgroud)

Shell Special Parameters的手册中:

_

(下划线。)在 shell 启动时,设置为用于调用在环境或参数列表中传递的正在执行的 shell 或 shell 脚本的绝对路径名。随后,在扩展后扩展到上一个命令的最后一个参数。还设置为用于调用在导出到该命令的环境中执行和放置的每个命令的完整路径名。检查邮件时,此参数保存邮件文件的名称。


cuo*_*glm 4

在之后添加此行set -o vi

bind -m vi-command ".":yank-last-argument # or insert-last-argument
Run Code Online (Sandbox Code Playgroud)

然后你可以像在 emacs 模式中一样使用Alt+ 。.

或者使用历史扩展,在两者中工作:

!$:p
Run Code Online (Sandbox Code Playgroud)

  • 它确实输出最后一个命令参数,但不会像 emacs 模式那样循环显示所有最后使用的命令参数。在 vi 模式下,您总是会一遍又一遍地获取最后一个参数。这没什么帮助。 (4认同)
  • 根据[文档](http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC13),正确的命令是`yank-last-arg`。 (2认同)