Small Sharp Software Tools

Harness the Combinatoric Power of Command-Line Tools and Utilities

9. Command Line Shortcuts

Tagged with Bash Shortcuts

Published February 5, 2021

Learn some handy command-line shortcuts to speed up your workflow

Transcript

Let’s look at some handy shell shortcuts to speed up your workflow. To keep this lesson short, I’m only going to cover a handfull here, and we’ll get into more complex ones in future videos.

First, you’ve probably already noticed that pressing the up arrow on your keyboard brings up the previous command you typed. Pressing it repeatedly moves back through your previous commands. Pressing down reverses the direction.

You can press CTRL+A to jump to the beginning of the line, so you can add something to the front of the command.

$ mkdir -p /tmp/this/is/a/long/structure
  ^

Pressing Esc+F will jump forward one word:

$ mkdir -p /tmp/this/is/a/long/structure
       ^

and pressing Esc+b will jump back one word.

$ mkdir -p /tmp/this/is/a/long/structure
  ^

On some terminals you can use the Alt key instead of Esc, but this is a configuration option you’ll need to explore.

You can press CTRL-E to jump to the end of the line.

$ mkdir -p /tmp/this/is/a/long/structure
                                       ^

If you decide you don’t want to execute this command, you can use CTRL-E to move to the end of the line and clear it with CTRL-U.

$

CTRL-U deletes every character before the cursor.

You can also press good old reliable CTRL-C to break out of the line, just like you’d use it to

$ ping google.com

PING google.com (216.58.192.206): 56 data bytes
64 bytes from 216.58.192.206: icmp_seq=0 ttl=114 time=42.153 ms
64 bytes from 216.58.192.206: icmp_seq=1 ttl=114 time=32.247 ms
64 bytes from 216.58.192.206: icmp_seq=2 ttl=114 time=37.486 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 32.247/37.295/42.153/4.046 ms

Pressing CTRL+L clears the screen, even in the middle of typing a command. I use this one a lot.

When you type history you see the commands you typed. You also see numbers next to each command.

$ history
1   cd /var/www
2   cd /usr/bin
3   tree /usr/bin
4   du -h /usr/bin
5   history
6
7   mkdir -p /tmp/this/is/a/long/structure
8   ping google.com
9   history

You can use these numbers to execute a previous command. Type the exclamation point, followed by the number, and the command runs again.

$ !10
mkdir -p /tmp/this/is/a/long/structure

Sometimes you just want to run the last command again. Typing out two exclamation points executes the last command again:

$ !!
mkdir -p /tmp/this/is/a/long/structure

Finally, pressing Ctrl-R lets you perform a reverse search, which is sometimes a lot faster than cycling through your history with the up arrow. Type part of the command and the command completes.

That’s all for this time. See how you can put these shortcuts to use in your own projects.