This expands somewhat on the !! trick mentioned in this answerthis answer. There are actually a bunch of history-related commands that tend to get forgotten about (people tend to stab Up 100 times instead looking for a command they know they typed).
- The
historycommand will show a list of recently run commands with an event designator to the left !Nwill substitute the command associated with event designatorN!-Nwill substitute theNth most recent command; e.g.!-1will substitute the most recent command,!-2the second most recent, etc.- As mentioned in the other answer,
!!is shorthand for!-1, to quickly substitute the last command !stringwill substitute the most recent command that begins withstring!?string?will substitute the most recent command that containsstring
Word designators can be added on to a ! history command to modify the results. A colon separates the event and word designators, e.g. !!:0. The event designator !! can be abbreviated to just ! when using a word designator, so !!:0 is equivalent to !:0.
!:0will get the command that was executed!:1will get the first argument (and!:2the second, etc.)!:2-3will get the second and third arguments!:^is another way to get the first argument.!:$will get the last!:*will get all arguments (but not the command)
Modifiers can also be appended to a ! history command, each prefixed by a colon. Any number can be stacked on (e.g. !:t:r:p).
h-- Line up to the base filenamet-- Only the base filenamer-- Line up to the filename extensione-- Only the filename extensions/search/replacement-- Replace the first occurrence ofsearchwithreplacementgs/search/replacement-- Replace all occurrences ofsearchwithreplacement