how to recall last argument in bash with vi setting
There are various method to get the last argument of last command:
1. inputrc: insert-last-argument & yank-last-arg
Copy the following code into your ~/.inputrc
file
set editing-mode vi
# Insert Mode
set keymap vi-insert
"\e.":yank-last-arg
"\e_": yank-last-arg
You can use my inputrc file. And here the inputrc manual for insert-last-argument
and yank-last-arg
2. Word Designators: !!:$ & !$
For example:
┌─ (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
The manual of Shell Word Designator shows:
!!:$
designates the last argument of the preceding command. This may be shortened to !$.
0 (zero)
The 0th word. For many applications, this is the command word.
n
The nth word.
^
The first argument; that is, word 1.
$
The last argument.
%
The word matched by the most recent ‘?string?’ search.
x-y
A range of words; ‘-y’ abbreviates ‘0-y’.
*
All of the words, except the 0th. This is a synonym for ‘1-$’. It is not an error to use ‘’ if there is just one word in the event; the empty string is returned in that case. x
Abbreviates ‘x-$’
x-
Abbreviates ‘x-$’ like ‘x*’, but omits the last word.
3. Shell Special Parameters: $_
For example:
┌─ (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) ->
└─ #
In the manual of Shell Special Parameters:
_
(An underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
Add this line after set -o vi
:
bind -m vi-command ".":yank-last-argument # or insert-last-argument
Then you can use Alt+. like in emacs-mode.
Or use history expansion, working in both:
!$:p