You should read through man bash on your system, particularly the section on Readline, as this is bash's interactive input mechanism. The Bash manual at gnu.org has a nice section on Commandline Editing that will help fill in some of the gaps.
By default, you can use these to move around on the commandline (just listing a few here; see the link for a full list):
- Ctrl+a moves to the beginning of the line (or Home depending on terminal settings)
- Ctrl+e moves to the end of the line (or End ...)
- Meta+f moves forward a "word"
- Meta+b moves backward a "word"
You can use these to "kill" (aka "cut") text from a line:
- Ctrl+k kills ("cuts") text from the current cursor position to the end of the line
- Ctrl+y yanks ("pastes") the most recently killed text back into the buffer at the cursor
So you can combine these to select a chunk of some commandline you want to repeat, kill it, then paste it to the end of your next command.
Now, to make it even more fun, let's consider bash's History Expansion. This is that
!! that Studer's answer mentions. History expansion breaks down into event designators, word designators, and modifiers.
Event designators look like this (again, see the links for the full list):
! - starts a history substitution
!n - the n-th command in bash's history list, for some integer n (works for negatives too)
!! - the preceeding command; equivalent to
!string - the most recent command starting with string
Word designators select certain parts from an event. Use
: to separate the event from the word designator. Words are numbered from 0 starting at the beginning of the line, and inserted into the current line separated by single spaces.
$ - designates the last argument (eg
!!:$ is the last arg of last command; can be shortened to
n - designates the n-th word (eg
!str:2 is the 2nd arg of the most recent command starting with str;
!!:0 is the command of the last command)
So, to follow up on your example, if your last command is
mkdir /some/really/long/path, just running
!! will rerun that command again. But you want to
cd into that path instead:
$ cd !$
Now let's say you perform some other commands and then want to refer back to that path again. If that was the last mkdir command you ran, you can use this to repeat that path:
$ tar czf ~/foo.tgz !mkdir:$