83

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?

10 Answers 10

96

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 d

This 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.

Sign up to request clarification or add additional context in comments.

5 Comments

Just an additional comment here, at least with Oh my Zsh after typing the ! argument you can expand it out hitting tab.
On your last line, did you mean !:0?
@babbata, nice catch ! I have fixed this in the body. Thanks :)
Could you add a link to canonical documentation about this please? Then the answer is perfect :)
@robrecord done, and there is much more to learn than my answer there :)
57

I just tested and it seems you can do it the same way as in bash: !$.

11 Comments

pretty good, but I loved how in bash I could just press "ALT ."
alt-period? İ think I've heard that before, but it doesn't work on my machine.
@Kevin: Your meta key mapping may be screwy. What does ALT+f do? What about ESC+.?
@Carlo Zsh has that same key binding in its default setup.
@kentor I believe it's setopt nohistverify
|
22

!* gives you ALL the arguments of the last command.

Example:

% echo hello world  
hello world

% echo !*  
(expands to)-> % echo hello world
hello world

Comments

18

<esc>. also works out of the box with zsh and oh-my-zsh.

1 Comment

and this way you get to see the argunent before running it!
16

add bindkey '\e.' insert-last-word to your .zshrc

- sp3ctum, in comment here

Comments

14

!$ gives you the last parameter of the previous command.

Example:

$ echo hello world
hello world
$ echo !$
echo world
world

Comments

10

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"

4 Comments

Or add bindkey '\e.' insert-last-word to your .zshrc.
@sp3ctum your comment is the best answer IMO
I realise it's been ages, but @sp3ctum solution seems to be the only one working for me. If you add it as an answer I'll accept it
did not work for me, I am using mac book pro 2017
3

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

Comments

2

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

Comments

2

Just to expand on @Charles Gueunet answer;

  • !! - repeats the entire last command

This 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.