Command Line Shortcuts

Some command line shortcuts that might make your life easier. They’ve helped me, but sometimes I forget so I decided to group some of my favorites here.

To be specific, we’re talking bash. As a common default shell, there’s a good chance it’s what you’re using. So a lot of this can be found inside the bash manpage. If you’re using something else, I can’t say what of this might be compatible…

Event Designators

Ever need to repeat the last command? Easy, just use !!

$ echo some super-duper-annoy-to-retype command
some super-duper-annoy-to-retype command

$ !!
echo some super-duper-annoy-to-retype command
some super-duper-annoy-to-retype command

$ echo "!!" > what-was-that.txt
echo "echo some super-duper-annoy-to-retype command" > what-was-that.txt

$ cat what-was-that.txt
echo some super-duper-annoy-to-retype command

This is extremely handy if you meant to run the last command as sudo, just run sudo !!

Note, when you use !! the first line of output is actually the expanded command.

But don’t stop at just shortcutting the last command, you can pull up any command from your histroy with !-n where n is the nth most-recent command.
So !! is the same as !-1, the second to last would be !-2 and so on

$ echo this
this

$ echo that
that

$ !-2
echo this
this

Word Designators

Suppose we want to grab a specific word out of a recent command. For simplicity, we’ll start with assuming we want a word from the last command, and that command will be

echo one two three four five six seven eight nine ten

To get any nth word (starting from zero, which would be the command):

$ echo !!:0
echo echo
echo

$ echo !!:6
echo six
six

First argument:

$ echo !!:^
echo one
one

Last argument:

$ echo !!:$
echo ten
one

This is great for reusing the filepath from the end of a previous command. Suppose you ls‘d when you meant to less:

$ ls file.txt 
file.txt
$ less !!:$
less file.txt 

A better solution for fixing mistyped commands is discussed later in this post.

Range of arguments:

$ echo !!:3-6
echo three four five six
three four five six

All the arguments

$ echo !!:*
echo one two three four five six seven eight nine ten
one two three four five six seven eight nine ten

Now to twist things up a bit. For the designators that begin with ^, $, or * (and -, or %, but not discussed here), you don’t need to include the colon.

So !!:$ for the last argument of the last command can be simplified to !!$. Futhermore, when referencing the last command (!!), you don’t need both exclamation points, !$ will do.

You can also change up the event designator to get the nth word from the 3rd last command, but I think at that point the shortcut is lost 🙂

Brace Expansion

Need a range of numbers?

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10

1 to 10 by 2s?

$ echo {1..10..2}
1 3 5 7 9

Note, this requires bash v4 which is greater than the default version on OS X.

Also works with the alphabet

$ echo {b..p}
b c d e f g h i j k l m n o p

Need more of a set list? Use commas. This is handy for creating multiple files or directories at once

touch {dog,puppy}.txt

Search-and-replace

Remember earlier when you ls‘d a file, and meant to less it? Here’s a better fix, since it works with any number of arguments:

$ ls file.txt 
file.txt
$ ^ls^less^
less file.txt 

With this method, you’ll only replace the first instance of the word, so it’s great for fixing commands. For example, if you filename had ‘ls’ in it, you’d still be safe.

$ ls ls.txt 
ls.txt
$ ^ls^less^
less ls.txt 

There’s a more flexible search-and-replace syntax that tacks on to those event designators (! commands from above). To start, here’s the equivalent command to the ^^^ style

$ ls ls.txt 
ls.txt
$ !!:s/ls/less
less ls.txt 

What if you do want to replace all instances of the word?

$ !!:gs/ls/less

Repeat the last replacement

$ !!:&

Of course, it can be nerve-racking to do an on-the-fly replacement on a potentially destructive command. Wouldn’t it be nice if you could preview the new command first? Well guess what…

$ !!:p:gs/ls/less

:p will print the new command without executing it. Plus, it’ll put the new command in your history, so you can just hit the up arrow to load it back up if it looks good.


If most of this is new to you, I highly recommend working some into your regular workflow.
If only some was new, you might find cooler tricks in the manpages.
If this is all old hat, teach me something cool in the comments!

^

Leave a Reply

Your email address will not be published. Required fields are marked *