bash command reuse

When you get tired of typing the same argument over and over

Alt + . cycles through previous args

On the command line you can use substitutions like these

> echo 1 2 3 4
# reuse the 3rd argument of previous command
> echo !:3

> ls -l /path/to/really/long/dir
# last command line argument of last command
> cd $_

> echo 1 2 3
# last word on previous command line
> echo !!$

> cd /path/to/really/lonZdirectory/
# reuse prev command by fixing lonZ to long (only works on first occurrence if there is more than one occurrence)
> ^lonZ^long^
# replace all occurrences
> !!:gs/replaceall/withthis/


push and pop your way around the shell

On the bash shell, it’s helpful for me have a list of directories at my disposal. That
way I can easily jump from one directory to another without having to type it all in.
Even with tab completion, that’s a pain.

Hence pushd.

pushd is not well documented, but basically you can push onto a stack, any directory
you like. It’s like cd, but the directory is added to a list of directories.

Suppose you have three directories


If you want to change to the first directory and add it to the stack,
> pushd ~/project1

Then to change to the tutorial directory,
> pushd ~/tutorial

To see what’s in your stack. The most recent directory is at the top of the stack.

> dirs
~/tutorial ~/projects ~

Now here’s the neat part. Suppose you add a third directory to the stack
> pushd ~/code
> dirs
~/code ~/tutorial ~/projects ~

To switch between the two most recent directories, just type

> pushd

To switch to the third directory (projects), indicate which directory you want
by adding ‘+n’ to the end.

For example, this will change you to the projects directory.

> pushd +2

Popd basically throws things off the stack, so if you want to pop off the
most recent directory

> popd

Which will dump you into the next most recent directory. Play around for a few minutes and you’ll see
how useful this is.