Some tips and tricks to make the terminal less frustrating

Most of us have tons of experience with operating systems that use a graphical user interface (GUI), such as Windows and MacOS. Using the GUI, you can easily see the files that exist in each directory, and the applications that are available in your “Program Files” or “Applications” directory. As you’re working within an application, the available options are displayed in menus or pop-up windows.

Windows 3.1

Things can feel more difficult when working in the terminal. That list of files and applications doesn’t appear unless you know how to make it appear. The available options within applications aren’t staring you in the face. But, the cursor is. And to make matters worse, a simple typo can make things stop working, and it’s up to you to figure out why!

Staring you in the face

However, there are some simple tips and keyboard shortcuts that can make life at the terminal much, much easier!

Avoid fat fingers with tab-complete

Aunt Marge's finger blows up

Use tab-complete to go faster and avoid typos!

In the terminal, hitting <tab> will attempt to complete whatever you’re typing with the name of a program or file. If the completion is unambiguous (in other words, it can only be completed with one option), hitting <tab> will complete with that option.

unambiguous tab complete

However, if there is more than one possible option that could be filled, hitting <tab> won’t be able to complete the full name. Instead, hitting <tab> twice (<tab><tab>) will show all of the possible names that can complete what you’ve started typing.

ambiguous tab complete

Use tab-complete to list files in your directory or commands in your PATH

One cool side effect of this feature is that you can see all possible commands by hitting <tab><tab> before typing anything. This probably won’t be too helpful because there are so many available commands. But, you could type a <tab><tab> to see all of the commands beginning with ‘a’, type b <tab><tab> to see commands starting with ‘b’, etc.

Once you’ve typed a command, hitting <tab><tab> (before typing anything else) will show files and directories within the present working directory. This is an easy way of seeing the contents of your directory without typing a separate ls command. (In some shells, tab complete will only show valid completions for the current command. For example, hitting <tab><tab> after typing cd might only show directories since you can’t change directory into a file.)

Get back to where you once belonged

Beatles - Get Back image from IMDb

Frequently working in the terminal, you will work on files located in a completely different directory. If the location of the new directory is different enough, it can be hard to get back to the previous directory where you started.

Luckily, there are options that make navigating back and forth between different directories simpler. The easiest option, in my experience, is to type cd - to get back to the previous directory. There are other options, like pushd and popd, but cd - (coupled with more tricks below) is all I ever use.

cd to the prior directory

Reduce, Reuse, Recycle

3R's

Often, you’ll find that you need to run the same command that you ran earlier, perhaps with some small changes. You’ve already spent time and effort meticulously crafting that command, and it would be nice to avoid doing that work all over again. Sure, you could grab the mouse, scroll and select the command, before finally copying and pasting. But that’s almost as much work as re-typing it! Thankfully, there are even easier ways to reuse the same command over again.

Often the easiest method is to hit the “up” arrow on the keyboard, which will cycle through the commands that you’ve entered, starting with the most recent. If you’re trying to reuse one of the last 5 or 10 commands, this works pretty quickly.

go through previous commands

If the command that you’re hoping to reuse happened a bit further back, it might be easier to start a “reverse search”. Hitting ctrl-r and beginning to type the command (or a unique / distinctive portion of the command) will show the most recent match to the characters that you’ve typed. After typing several characters to match, hitting ctrl-r a second time will show the penultimate match, and additional ctrl-r’s will go even further back in your command history.

go through matching commands

Speaking of your command history, you can type the command history to show a numbered, ordered list of commands that you’ve run previously. This can be very useful if you want to remember the specific order in which you ran several commands. (Copy and paste them into a bash script if you’re using them often enough!)

Changes ahead

sharp turn

Sometimes, you need to change the command currently on the command line. Whether you are reusing a previous command and need to change it up a bit, or you need to change a typo in a command that you’ve just typed, there are several quick keyboard combinations that help you make changes quickly.

It can be super-helpful to jump to the beginning of the line (to change the command or its flags) or jump to the end of the line (to change the input or output filename). You can do this using ctrl-a (beginning of line) and ctrl-e (end of line).

go to beginning or end of command

Rather than using the arrow keys to go back and forth one character at a time, you can hit esc followed by b (to go back one field) or f (to go forward one field). You can also hit esc followed by delete to delete one field at a time.

Be aware that these key combinations involving esc are performed differently than ctrl-a and ctrl-e above. For the esc sequences, you release esc before hitting b, f, or delete. On the other hand, the ctrl sequences require that you hold ctrl while hitting a or e. (I have no idea why!)

go back or delete each word

Use the GUI

efficient, not lazy

Let’s be honest… sometimes it’s still easier to use the GUI. There are relatively easy ways to switch between the bash terminal and the GUI. Sometimes, switching back-and-forth with the GUI isn’t lazy… it’s efficient.

You can use open (on macOS) or explorer (on Windows) to open a directory in the GUI. To open your current directory in the terminal, type open . or explorer . (the dot points at the current directory).

On macOS, it is possible to open files in GUI applications in terminal, but it can be a bit confusing (for example, open -a /Applications/Microsoft\ Word.app MyWord.docx).

To go back to the terminal from the GUI, you can drag-and-drop a file or directory onto the terminal window, and it will insert the full path to that item into the terminal.

open and drag-and-drop

Now you’re ready to finish the Kessel Run in less than 12 parsecs

warp speed

Practicing these tips, tricks, and keyboard shortcuts should really speed up your work in the terminal. If you stick with it, you’ll soon be navigating the terminal like the Millenium Falcon navigates an asteroid field!