How to use arguments from previous command?

Viewed 102534

I know that Esc + . gives you the last argument of the last command.

But I'm interested in first argument of the last command. Is there a key binding to do so?

On the same lines, is there a generic way of getting the nth argument from the last command? I know that in a bash script, you can use $0, $1 etc., but these don't work on the commandline.

Also, what about iterating through the 0th argument of previous commands, like we can do with the last argument by continuously pressing Esc + .?

12 Answers

Tested on Ubuntu 18.04


To insert previous arguments:

  • Alt+.: insert last argument from last command.
  • Alt+#+.: insert #nth last argument from last command.
  • Alt+- , # , Alt+., zsh: Alt+-+#+.: insert #nth first argument from last command.

In Linux you can repeat commands to go back in history

Example:

Last command is:

mv foo bar
  • Alt+0+.: insert first argument of last command = mv
  • Alt+2+.: insert last 2nd argument of last command = foo
  • up , Ctrl+w: last command without the last word = mv foo

General shortcuts

  • Ctrl+w: removes last word from cursor
  • Alt+d: removes next word from cursor
  • Ctrl+k: cuts everything after cursor
  • Ctrl+u, zsh: Alt+w: cuts everything before cursor
  • zsh: Ctrl+u: cuts the entire command (In bash you can combine Ctrl+u , Ctrl+k)
  • Ctrl+y: paste characters previously cut with Ctrl+u and Ctrl+k
  • Ctrl+_: undo last edit (very useful when exceeding Ctrl+w)
  • Ctrl+left: move to last word
  • Ctrl+right: move to next word
  • home or Ctrl+a: move to start of line
  • end or Ctrl+e: move to end of line

To iterate through the arguments in a previous command

only works in zsh

run or add this to your ~/.zshrc

autoload -Uz copy-earlier-word
zle -N copy-earlier-word
bindkey "^[:" copy-earlier-word

Now use Alt+. to go as back as you want, then use Alt+: to iterate through the arguments

Assuming last command is

echo 1 2 3 4 5
  • Alt+.: 5
  • Alt+.+:: 4
  • Alt+.+:+:: 3
  • Alt+.+:+:+:: 2
  • Alt+.+:+:+:+:: 1
  • Alt+.+:+:+:+:+:: echo

source: https://stackoverflow.com/a/34861762/3163120

To see all shortcuts available

  • bash: bind -lp
  • zsh: bindkey -L

I'm keeping this up-to-date here: https://github.com/madacol/knowledge/blob/master/bash-zsh_TerminalShorcuts.md

!^ will get you the first param, !$ will get you the last param, !:n will get you the nth element.

If you are on a mac you will tend to get extended characters with ctrl+letter. I have my right-of-space-bar-option key defined as meta in my terminal (iTerm2) set up. This means I use the key to navigate by word and pull parameters from previous commands.

For pasting 1th argument, press and hold down Alt key, and while it is down, hit the '1' key followed by the '.' key.

For pasting n-th argument, replace the '1' key above with the corresponding number key.

If this does not work, your terminal emulator may be catching the Alt key before it gets to shell. Some terminals (xfce4-terminal) allow turning off the "Alt-" shortcuts in the configuration file.

Credit to Jonas Eberle, I've fished this out from his comment to another answer here.

Related