Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Terminal Efficiency: Tab Completion and History

Recommended resources

Introduction

Welcome back! In our last lesson, you learned how to use man pages and the --help flag to find documentation for any command. This is a crucial skill for becoming self-sufficient on the command line. Now that you know how to find out what a command does, let's focus on how to use it faster.

Typing out long commands and file paths repeatedly is slow and prone to errors. Professional developers and system administrators rely on shell features to work efficiently. Today, we'll cover two of the most important efficiency boosters: tab completion and command history.

By the end of this 60-minute lesson, you will be able to:

  • Use tab completion to automatically finish commands, filenames, and directory paths.
  • Use command history to recall, search, and reuse commands you've already typed.

Mastering these skills will dramatically increase your speed and accuracy in the terminal, a huge benefit whether you're managing a server or deploying your Django projects.


The Art of Less Typing: Tab Completion

Tab completion is one of the most beloved features of the command line. It allows the shell to automatically fill in the rest of a command or filename for you after you've typed the first few letters. This not only saves typing but also helps prevent typos.

There are two main scenarios for tab completion:

  1. Unique Completion: If what you've typed has only one possible completion, pressing the Tab key once will fill in the rest of the name instantly.
  2. Multiple Options: If what you've typed could be completed in multiple ways, pressing Tab once might do nothing. Pressing it a second time will show you all the possible completions. You can then type another letter or two to make your choice unique and press Tab again.

This video provides a great introduction to the concept.

Beginner's Guide to the Bash Terminal

Let's watch this segment from the "Beginner's Guide to the Bash Terminal" by Joe Collins. It provides a clear, beginner-friendly demonstration of how tab completion works for navigating directories.

Watch from 08:57 to 10:23. Pay attention to the difference between pressing Tab once for a unique match and pressing it twice to see a list of possible options.

Tab completion doesn't just work for file and directory names; it also works for commands themselves. If you're not sure of the exact command name, you can type the first few letters and press Tab twice to see all available commands that start with that prefix.

Linux Terminal Tab Completion Example
This image shows what happens when you type `ls` and press `Tab` twice. The terminal lists all commands that begin with `ls`, helping you discover commands like `lscpu` or `lsblk`.

Do It Yourself: Practice Tab Completion

Let's put this into practice. Open your terminal and follow along.

  1. Complete a Path: Type cd /var/lo and press Tab. It should immediately complete to cd /var/log/. Press Enter.
  2. See Multiple Options: Now that you are in /var/log, type ls a and press Tab twice. Your system will show you all files and directories in /var/log that start with 'a' (like audit/ and anaconda/).
  3. Narrow It Down: Type u (so your line reads ls au) and press Tab again. It should complete to ls audit/.
  4. Complete a Command: Type dnf che and press Tab. It will complete to dnf check-. This command has multiple endings (check, check-update), so it stops there. Type u and press Tab again. It will complete to dnf check-update. (Press Ctrl+C to cancel the command without running it).

Don't Repeat Yourself: Using Command History

Your shell keeps a record of the commands you execute. This "history" is a powerful tool for reusing complex commands without retyping them.

The Basics: history and Arrow Keys

The simplest way to access your history is with the up and down arrow keys.

  • Up Arrow (↑): Recalls the previous command. Press it again to go further back in time.
  • Down Arrow (↓): Moves forward through the history, towards your most recent commands.

To see a numbered list of your recent commands, you can use the history command.

Beginner's Guide to the Bash Terminal

Let's return to the "Beginner's Guide" video, where Joe Collins explains how to use the up arrow and the history command.

Watch from 19:55 to 21:19. This will show you how to cycle through commands and view the history list.

Using the 'history' Command in Linux
As seen in the image, you can run `history` to get a full list, or `history 5` to see just the last five commands.

Searching and Re-Executing with Shortcuts

Scrolling through your history with the arrow keys is fine for recent commands, but what about a command you ran yesterday? This is where more powerful history features come in.

Reverse Search (Ctrl+R)
This is arguably the most useful history shortcut. It allows you to search backward through your history.

8 Bash Shortcuts Every Linux User Should Know

The video "8 Bash Shortcuts Every Linux User Should Know" has an excellent, quick demonstration of reverse searching.

Watch from 06:59 to 08:27. Focus on how to start the search with Ctrl+R, type a fragment of the command you remember, and press Enter to execute it or Ctrl+R again to cycle through matches.

History Expansion (!)
Bash also provides a powerful syntax called "history expansion" using the exclamation mark (!) to quickly re-run commands. This is best explained with a text resource that you can refer back to.

The Definitive Guide to Bash Command Line History

The article "The Definitive Guide to Bash Command Line History" from catonmat.net is a comprehensive resource. We will focus on the most practical parts about executing commands from your history.

Please read the section titled Event Designators. It's about a third of the way down the page. Focus on understanding these four key shortcuts: !!: Executes the previous command. !N: Executes the Nth command from the history list. !string: Executes the most recent command that starts with string. ^string1^string2^: Takes the last command, replaces the first occurrence of string1 with string2, and executes it.

Line Editing Shortcuts
Once you've recalled a command from your history, you often need to edit it. Instead of using the arrow keys to move one character at a time, you can use keyboard shortcuts to jump around the line.

An Illustrated Guide to Shell Magic: Typing Less & Doing More

Adafruit's "An Illustrated Guide to Shell Magic" has a very handy table of these shortcuts. Learning these will make editing commands much faster.

Find the section titled Line Editing and review the table of shortcuts. Try to memorize Ctrl+A (start of line), Ctrl+E (end of line), and Ctrl+K (delete to end of line).

Do It Yourself: Become a History Pro

Time to combine these skills. This is where your 80% practical preference comes into play. Open your terminal and try these tasks.

  1. Run a Few Commands: Execute the following commands, including the typo.

    date
    ls -l /etc/dnf/dnf.conf
    ehco "I love Fedora"
    whoami
    
  2. Fix a Typo: Your ehco command failed. Instead of retyping it, use the substitution shortcut to fix it and run it again.

    ^ehco^echo^
    
  3. Search and Execute: Now, let's find that long ls command without scrolling.

    • Press Ctrl+R.
    • Start typing dnf.conf. The command ls -l /etc/dnf/dnf.conf should appear.
    • Press Enter to execute it.
  4. Use History Numbers:

    • Type history. You will see a numbered list. Find the number next to the whoami command you ran. Let's say it's 1024.
    • Execute it again by its number:
      !1024 
      
      (Use the actual number from your history output).
  5. The sudo !! Trick: This is a classic time-saver. Try to list the contents of the /root directory, which is protected.

    ls /root
    

    You'll get a "Permission denied" error. You forgot to use sudo. Instead of retyping, just run:

    sudo !!
    

    The !! is replaced by your last command (ls /root), so the shell executes sudo ls /root. Enter your password when prompted.

  6. Practice Line Editing:

    • Press the up arrow until you get back to ls -l /etc/dnf/dnf.conf.
    • Press Ctrl+A to jump to the beginning of the line.
    • Press Ctrl+E to jump to the end of the line.
    • Press Ctrl+A again. Use the right arrow to move to the ls command and add the d flag to see directory info: ls -ld /etc/dnf/dnf.conf. Press Enter.

Conclusion

Excellent work! You've just learned the core techniques that separate beginners from proficient command-line users. By making these shortcuts a habit, you'll work faster, make fewer mistakes, and feel much more comfortable in the terminal.

Key Takeaways:

  • Tab Completion: Your best friend for avoiding typos and discovering commands. Use a single Tab for unique completions and a double Tab to see all options.
  • Command History: Don't retype, reuse!
    • ↑ and ↓ arrows are for recent commands.
    • Ctrl+R is for searching your entire history.
    • !! re-runs the last command (perfect for sudo !!).
    • !N re-runs a specific command by its number from the history list.
    • ^old^new^ is the fastest way to fix a typo in your last command.
    • Ctrl+A and Ctrl+E let you jump to the start and end of a line for quick editing.

In our next lesson, we'll tackle another fundamental skill: searching for files. You'll learn how to use the find command to locate files and directories based on their name, type, size, and modification time—an indispensable skill for managing projects and system configurations.