6.6 Tab Completion and Shell Readline Shortcuts
Right, let’s talk about one of the single greatest productivity boosts you’ll ever get from your shell: not typing things. I’m serious. The measure of a shell wizard isn’t how fast they can type ls -lahtr, but how little they have to type to get the job done. This is the magic of Tab completion and Readline shortcuts. Master these, and you’ll feel like you’ve developed a mild superpower. Your fingers will barely leave the home row, and you’ll look at people who hunt for the arrow keys with a mix of pity and confusion.
The Magic of Tab Completion
This is your shell’s best guess. You start typing a command, a filename, or an option, and you hit Tab. If your shell can unambiguously figure out what you mean, it completes it for you. If there are multiple possibilities, it either shows them to you or, in most modern setups, cycles through them.
Let’s say you have a directory with a file called supercalifragilisticexpialidocious.txt. You do not type that. You type cat sup and hit Tab. If no other file starts with sup, bash or zsh will instantly complete the whole ridiculous name. You’ve just saved yourself from carpal tunnel and a typo.
The real power comes when it’s not unambiguous. Try this:
ls /usr/bin/py<Tab>
On most systems, you’ll get a list of possibilities like python, python2, python3, python3-config, etc. Now, you can type a few more characters to disambiguate (e.g., 3) and hit Tab again to complete it. In zsh, and bash with some configurations, you can just keep hitting Tab to cycle through the options—far more efficient.
Why this works: Your shell has a built-in completion system. It’s not just guessing filenames; it’s context-aware. When you type git sta<Tab>, it knows you probably mean git status and not a file named status. This is managed through sophisticated completion scripts, which are a rabbit hole we’ll explore later. For now, just appreciate the wizardry.
Beyond Files: Command and Option Completion
This is where things get spicy. Both bash and zsh can complete commands themselves and their flags.
sudo apt up<Tab><Tab>
You’ll likely see update and upgrade as options. This saves you from having to remember the exact syntax of every single command.
Even better, try this with docker or kubectl commands. Their completion systems are incredibly detailed, often telling you which arguments expect a container name, an image, or a file path. It’s like having built-in documentation at your fingertips.
Pitfall: The quality of completion is entirely dependent on the completion scripts installed on your system. If you apt install a new tool and its tab-completion is lackluster, it’s not the shell’s fault. Blame the package maintainer (quietly, to yourself). You can often find better completion scripts online or write your own.
Readline: Your Text Navigation Toolkit
Here’s a secret: when you’re typing a command, you’re not just typing into a dumb prompt. You’re using the GNU Readline library. This is the engine under the hood that handles line input, and it’s packed with shortcuts. These work in any application that uses Readline, which is most shells (bash, zsh), and even other tools like the Python REPL.
Let’s get to the good stuff. Memorize these. They will change your life.
Essential Editing Shortcuts
Ctrl + a: Jump to the Abeginning of the line.
Ctrl + e: Jump to the End of the line.
This alone should make you abandon the arrow keys forever. Need to fix a typo at the start of a long command? Ctrl+a. Forgot to add sudo? Ctrl+a, type sudo .
Ctrl + u: Unceremoniously kill the line from the cursor backwards to the start. It cuts the text, saving it in a “kill ring” so you can…
Ctrl + y: Yank that text back. So, Ctrl+u followed by Ctrl+y is a quick way to clear a line but keep it handy. Or, if you typo the first part of a command: Ctrl+u, fix the typo, Ctrl+y to get the rest back.
Ctrl + w: Delete the word West of the cursor (i.e., backwards). Perfect for quickly fixing the last argument you typed. Alt + d: Delete the word Deast of the cursor (i.e., forwards).
Ctrl + k: Kill the line from the cursor forwards to the end. Great for when you realize the last half of your command is wrong.
Ctrl + _: Undo the last edit. Yes, undo! This is a lifesaver.
The History Commands
Ctrl + r: Reverse-i-search. This is the big one. Hit Ctrl+r and start typing any part of a command you’ve used before. It will search your history and instantly complete it. Hit Ctrl+r again to cycle through older matches. Once you find it, press Enter to run it or press a directional key (e.g., right arrow) to stop searching and edit the command.
Ctrl + p: Previous command (same as up arrow). Ctrl + n: Next command (same as down arrow).
Pro Tip: In zsh, the history search is even smarter. You can just type a part of a previous command and press the up arrow, and it will filter your history to only commands containing that text. It’s glorious.
The Absurd Part: .inputrc
Here’s the thing the manual often buries: you can configure the living daylights out of this. All Readline behavior is controlled by a file in your home directory called .inputrc. Want to make Tab cycle through options instead of listing them? Want to make a shortcut that inserts the current date? You can do that.
For example, to make Shift-Tab cycle backwards through completions (which is oddly not the default), you can add this to your ~/.inputrc:
# ~/.inputrc
"\e[Z": menu-complete-backward
Why isn’t this the default? Who knows. The designers giveth, and they taketh away. The important thing is that you can usually fix it. The shell is your workshop. Tinker with it. Make it work for you, not the other way around. Now stop hunting for the backspace key.