Reputation: 4148
I'm using zsh with the oh-my-zsh framework of Robby Russell. How can I create a shortcut or something to repeat the last part of a command?
For example, if I type:
mv something in/this/difficult/to/type/directory
Is there any way to easily get this: in/this/difficult/to/type/directory
?
Upvotes: 82
Views: 24657
Reputation: 2046
Just to expand on @Charles Gueunet answer;
!!
- repeats the entire last commandThis is useful if you forgot to add sudo
to the start of the command. Trivial example:
$ cat /root/file
Permission denied
$ sudo !!
here's the conent
Upvotes: 2
Reputation: 1918
Wether you are in bash or zsh, you can use the !
operator to recover arguments of your previous command:
If we take: echo a b c d
as an example
!$
- the last argument: d!:*
- all the arguments: a b c d (can be shorten !*
)!:1
- the first argument: a (same as !^
)!:1-3
- arguments from first to third: a b c!:2-$
- arguments from the second to the last one: b c dThis last point answer you question, you can take the last part of your command.
Note: !:0
is the last command executed, here it would be echo in our example
The corresponding documentation can be found on the gnu website, the whole context gives much more resources than this comment.
Upvotes: 94
Reputation: 634
Another way to reference the last argument of a command is with $_
mkdir 'a_new_directory'
cd $_
The above code will move you into the directory you just created.
$_
can also do other things. Reference: https://unix.stackexchange.com/questions/280453/understand-the-meaning-of
Upvotes: 3
Reputation: 2669
If you get here looking for pasting last word from last command on an zsh interactive shell, maybe this is your answer.
There is a default shortcut already configured in any zsh (or maybe if you have installed oh-my-zsh, i'm not really sure).
To check if you have this shortcut installed, search in bind keys for insert-last-word
$ bindkey -L | grep insert
...
bindkey "^[." insert-last-word
bindkey "^[_" insert-last-word
...
When using this keyboard shortcut, you can paste last word used in last command.
Example:
$ echo a b c d e f g
a b c d e f g
...
$
[use shortcut]
$ g
Upvotes: 2
Reputation: 822
!*
gives you ALL the arguments of the last command.
Example:
% echo hello world
hello world
% echo !*
(expands to)-> % echo hello world
hello world
Upvotes: 22
Reputation: 261
add bindkey '\e.' insert-last-word to your .zshrc
- sp3ctum, in comment here
Upvotes: 16
Reputation: 1276
I ran into this too - I've always used Alt.
for insert-last-word in bash. Found where oh-my-zsh overrides this.
In lib/key-bindings.zsh, comment out this and it should work like in bash:
bindkey -s '\e.' "..\n"
Upvotes: 10
Reputation: 56089
I just tested and it seems you can do it the same way as in bash: !$
.
Upvotes: 57
Reputation: 274612
!$
gives you the last parameter of the previous command.
Example:
$ echo hello world
hello world
$ echo !$
echo world
world
Upvotes: 14