When running commands, sometimes maybe you need to run a command with the argument from the last command. How can you do this?
Of course, excepting the use of the arrow keys and Del keys: ↑←←←...←→→DelDelDel...Del<new_command>
.
When running commands, sometimes maybe you need to run a command with the argument from the last command. How can you do this?
Of course, excepting the use of the arrow keys and Del keys: ↑←←←...←→→DelDelDel...Del<new_command>
.
For example, if you run this command:
most likely you'd want to go in the recently created directory. You can do this using next "shortcuts":
cd
Esc. - typecd
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+. - typecd
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.
<command> !*
<command> !$
Examples:
If you want a single argument from a list of arguments from the previous command, you can use
<command> !!:<argNumber>
Example:
Mentioned
!*
and!$
is a good thing, but when you need to do a little editing readline shortcuts come to help!For instance, instead of the countless ↑←←←...←→→DelDelDel...Del you can just press ↑ Ctrl-a(jump to the start of the line) Alt+d(delete to the end of the word)
For more info
man readline
and search forDefault key bindings
.To take the
n
th argument from your previous command, type Alt+n
+Alt_. For example, after:typing
echo
followed by Alt2Alt_ will give you this at the next prompt:The "screenshot" after you've hit Alt2 is:
You can repeat Alt_ multiple times to get the
n
th argument of previous commands in succession.Another useful idea is to define alias
r="fc -s"
. Then, you can do command substitutions: