How to repeat the last part of a previous command?
Asked Answered
C

10

77

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?

Condescendence answered 1/12, 2011 at 16:24 Comment(0)
M
89

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.

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

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

Slough answered 1/12, 2011 at 16:31 Comment(11)
pretty good, but I loved how in bash I could just press "ALT ."Condescendence
alt-period? İ think I've heard that before, but it doesn't work on my machine.Slough
@Kevin: Your meta key mapping may be screwy. What does ALT+f do? What about ESC+.?Beamends
Alt-f is some funny symbol, esc-. does the same symbol but inserts it at the cursor and doesn't advance. Part of it may be that I'm using the US-intl keyboard. I use vi-mode too, but I just checked and switching to emacs-mode doesn't fix it.Slough
@Condescendence Zsh has that same key binding in its default setup.Blasto
@Gilles, are you talking about ALT+.? at the moment, if I type ALT+. it outputs two dots and then goes up in the directory tree.Condescendence
@Condescendence Alt+. is in the default emacs keymap. If you don't have this binding, either your configuration overrides it (perhaps in oh-my-zsh) or you're using the vi keymap (which doesn't have this widget, insert-last-word, bound by default).Blasto
how do you prevent the command from repeating and just make it run? for example if you do echo !$ and press enter, you see the command again, and you would have to press enter a second time to make it actually run. this is annoying.Bias
@Bias I believe it's setopt nohistverifySlough
@Slough I used to use ALT+. (or I actually tended to use ESC+.) with zsh but I just moved to vi-mode prompt. The solution posted (thank you) works. Is there a way of getting a similar command working in zsh vi-mode?Quiroz
For ALT+. use bindkey '\e.' insert-last-word. Or, use bindkey -M viins '\e.' insert-last-word to apply just to vi (insert) mode.Genitourinary
A
22

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

Example:

% echo hello world  
hello world

% echo !*  
(expands to)-> % echo hello world
hello world
Antofagasta answered 22/11, 2014 at 6:17 Comment(0)
S
16

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

- sp3ctum, in comment here

Sostenuto answered 17/4, 2014 at 20:1 Comment(0)
O
15

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

Outlay answered 25/2, 2017 at 12:50 Comment(1)
and this way you get to see the argunent before running it!Allrud
G
14

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

Example:

$ echo hello world
hello world
$ echo !$
echo world
world
Gavra answered 1/12, 2011 at 16:30 Comment(0)
L
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"

Lian answered 10/2, 2012 at 23:0 Comment(4)
Or add bindkey '\e.' insert-last-word to your .zshrc.Fiden
@Fiden your comment is the best answer IMOPhilcox
I realise it's been ages, but @Fiden solution seems to be the only one working for me. If you add it as an answer I'll accept itCondescendence
did not work for me, I am using mac book pro 2017Horatio
A
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
Au answered 10/8, 2018 at 15:6 Comment(0)
S
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
Semination answered 23/5, 2020 at 15:7 Comment(0)
E
2

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

Earthling answered 22/2, 2023 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.