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?
To repeat the last action in a Microsoft Office program, press the F4 key or Ctrl + Y on a Windows keyboard.
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.
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.
I just tested and it seems you can do it the same way as in bash: !$.
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
!* gives you ALL the arguments of the last command.
Example:
% echo hello world
hello world
% echo !*
(expands to)-> % echo hello world
hello world
add bindkey '\e.' insert-last-word to your .zshrc
- sp3ctum, in comment here
!$ gives you the last parameter of the previous command.
Example:
$ echo hello world
hello world
$ echo !$
echo world
world
<esc>. also works out of the box with zsh and oh-my-zsh.
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"
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 /some/root/owned/thing/with/a/long/path
Permission denied
$ sudo !!
here's the conent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With