Run a command with the argument from the last command

For example, if you run this command:

mkdir long_path_here/new_dir

most likely you'd want to go in the recently created directory. You can do this using next "shortcuts":

  • cd Esc. - type cd and after press Esc followed by . (not in the same time). If the previous command has no arguments, you will get the previous command itself.
  • cd !* - in this case you will get all arguments from the previuos command. If the previous command has no arguments, you will get nothing.
  • cd Alt+. - type cd and after press Alt and . (in the same time). In fact, using this way and continuing to press . (without to release Alt), you will get the last argument for every command from history. If a command has no arguments, you will get the command itself.

In general: <command> Esc. or <command> !* or <command> Alt+..


There are a few shortcuts if you want all of the arguments from the previous command, or just the last argument.

  • For all of the arguments: <command> !*
  • For just the last argument: <command> !$

Examples:

ls foo/ bar/
ls !* # Gives the results of ls foo/ bar/

ls foo/ bar/
ls !$ # Gives the results of ls bar/

If you want a single argument from a list of arguments from the previous command, you can use <command> !!:<argNumber>

Example:

ls foo/ bar/ baz/
ls !!:2 # Gives the results of ls bar/
ls foo/ bar/ baz/
ls !!:1 # Gives the results of ls foo/