7

I use set -o vi setting in bash. The shorcut Alt+. (recalling last argument of previous command) here doesn't work as in emacs mode, so what is the equivalent for vi?

4

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
  • 2
    It does output the last command parameter but does not cycle through all last used command parameters, like emacs mode does. In vi mode you'll always get the last parameter over and over. It doesn't help much. – MaikoID Oct 6 '14 at 15:01
  • 1
    According to the documentation, the correct command is yank-last-arg. – jasonwryan Jan 5 '15 at 0:06
  • @jasonwryan: They're synonym. See: gnu.org/software/bash/manual/html_node/…. – cuonglm Jan 5 '15 at 1:37
9

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.

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.