Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat the last part of a previous command?

Tags:

zsh

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?

like image 902
Carlo Avatar asked Dec 01 '11 16:12

Carlo


People also ask

What key is repeat the last command?

To repeat the last action in a Microsoft Office program, press the F4 key or Ctrl + Y on a Windows keyboard.

How do I repeat the last command in Autocad?

In the command bar or Prompt History window, right-click, choose Recent Commands, and then choose the desired command. Press Ctrl + K, repeat until you get back to the desired command, and then press Enter.

How do I repeat the last command without using arrow keys?

Method 3: Use CTRL + P then CTRL + O Pressing CTRL + P will let you switch to the last command and pressing CTRL + O will let you execute the current line. Note: CTRL + O can be used as many times as you want.


8 Answers

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

like image 54
Kevin Avatar answered Oct 03 '22 08:10

Kevin


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

like image 37
Charles Gueunet Avatar answered Oct 03 '22 08:10

Charles Gueunet


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

Example:

% echo hello world  
hello world

% echo !*  
(expands to)-> % echo hello world
hello world
like image 21
Dave Avatar answered Oct 03 '22 06:10

Dave


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

- sp3ctum, in comment here

like image 22
Andrew Vandever Avatar answered Oct 03 '22 07:10

Andrew Vandever


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

Example:

$ echo hello world
hello world
$ echo !$
echo world
world
like image 25
dogbane Avatar answered Oct 03 '22 06:10

dogbane


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

like image 39
ha7ilm Avatar answered Oct 03 '22 08:10

ha7ilm


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"

like image 23
Brian Ray Avatar answered Oct 03 '22 07:10

Brian Ray


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 /some/root/owned/thing/with/a/long/path
Permission denied
$ sudo !!
here's the conent
like image 39
Arj Avatar answered Oct 03 '22 07:10

Arj