Linux Tab Completion Is Even Better Than You Think


Summary

  • The tab completion feature saves time and prevents errors by auto-completing commands and filenames.
  • Linux shells like bash and zsh offer powerful tab completion features that handle subcommands and options.
  • The git command showcases the full potential of tab complete with context-specific completions.

Linux is full of shortcuts, you just need to know what they are or where to look for them. Tab completion is one of those features that is great at its simplest, with hidden power that you may be unaware of.

What Is Tab Completion?

Tab completion is a fantastic little feature that adds convenience, saves time, and can even help to prevent mistakes. It’s one of the oldest forms of auto-complete in computing: press Tab on a command line and you’ll see a command or filename completed (if unambiguous) or a set of possible options (if ambiguous).

Here are some examples of how tab completion works.

Tab completion is a feature of your shell, and this article focuses on two of the most used: bash and zsh. Completion is highly configurable, and features vary between systems.

Type the first few letters of a command (try apr or mkd), then press Tab. If your shell finds only a single command beginning with those letters, it will auto-complete it by printing the remaining letters. Now, you can press Enter to run the command or continue typing to supply more arguments.

If your initial text matches more than one command, their names will print to the screen. Here’s an example showing the text rm followed by a Tab:

A Linux shell showing available commands beginning with the text "rm."

You can continue to type and press Tab to refine the list until it is unambiguous or you’ve typed your full command.

Tab completion also works for filenames that you supply as arguments. For example, you can type ls /u followed by Tab and your shell should complete it to ls /usr/. This makes it much faster to type longer paths, and it’s also far less error-prone.

This level of tab completion alone will transform your Linux experience. When I first learned about tab completion, it felt like magic, and I don’t think I could live without the feature today. But, as I discovered recently, tab completion offers so much more than just command and filename completion.

Tab Completion Is So Much More

For a start, try tab completion with a command like cd, in a directory containing both files and folders. You should notice that cd {start-of-directory}, then Tab, auto-completes the directory name, but cd {start-of-file}, then Tab, does not. The shell is clever enough to recognize that cd’s argument should be a path to a directory, not a file, so it auto-completes the former, but not the latter.

Your shell should include a trailing slash after completing a directory, even if you only need to supply the directory’s name. This lets you quickly tab-complete an entire path without having to stop and type each / between directory names.

By showing all matches when there are more than one, your shell can help you narrow down whatever you’re trying to type. But some shells take this further. For example, in zsh, on macOS, when I type ls Do followed by Tab, my shell prints two matches, Documents and Downloads:

The ls command with an argument "Do" showing auto-completions that read "Documents/" and "Downloads/."

If I now press Tab again, it completes to ls Documents/, and pressing Tab further times cycles between ls Documents/ and ls Downloads/. Note that this only happens when there is ambiguity. If you type ls Documents/, then press Tab, the auto-complete will print all files inside Documents rather than cycling to the Downloads directory.

Add bind TAB:menu-complete to your ~/.bashrc or TAB:menu-complete to your ~/.inputrc if you want this behavior.

To accept a completion when cycling, but continue tab completing—e.g. for a long path—you can press / at the end of the directory you want to select.

Tab completion can also handle options supported by the command you’ve typed. For example, try typing mkdir – followed by a Tab. Again, the results will depend on your environment, but either way you should be able to see a list of options that mkdir supports:

The mkdir command with auto-completed options including -m, -p, and -v.

On macOS, my shell shows single-letter options for mkdir, like -m and -p. Alongside each, it shows usage text that explains what the option does. On Ubuntu, my shell shows long options. It doesn’t print any usage info, but long options are more self-documenting anyway.

Related


5 Ways to Make Linux Commands Work the Way You Want

You don’t really know the full power of Linux until you can tweak your toolset.

The git Command Takes Completion to the Next Step

Auto-completing options were a real eye-opener for me. I was so used to using man or a –help option to show command usage, I had no idea my shell could do a lot of the work for me. But it was the git command that really showed me the true power of auto-complete, and it’s transformed the way I work since.

The git program is incredibly self-aware, so its auto-complete options effectively provide an alternative interface. You can start by exploring all of git’s subcommands by typing git followed by a tab:

The git command shows a list of subcommands, including add, blame, and checkout when tab is pressed.

But git auto-complete shows its full power when working in a git repository. Let’s say you’ve made a change to a single local file, like a README.md:

Example output from the git status command showing a file, README.me, has been modified.

Now type git add, then a Tab, and your shell should transform that into git add README.md. The git program has recognized that the add subcommand can be followed by a list of filenames to add them to the current commit. It’s aware that it only makes sense to add files that have actually changed, so it can auto-complete the full name of the only file that needs to be added.

Related


10 Basic Git Commands to Get You Started

Git can be intimidating for beginners, but the best way to learn is to dive in and start using it.

Likewise, once you’ve added a file, git commit then Tab will auto-complete its name, along with other files that you can commit immediately, including files you haven’t yet added:

Tab completion from git commit showing two modified files.

This completion even takes arguments into account. For example, once you’ve added a file to the index, you can unstage it using git restore –staged, as git status explains. At this point, git restore –staged followed by a Tab will show just README.md, the only file that is staged:

The git restore command auto-completing a filename which is the only one that can be restored.


Tab completion is so useful that you’ll find yourself using it all the time once you’ve discovered it. But it always pays to be curious: keep pressing Tab at different points in your command chain, and you’ll learn exactly how powerful this feature can be.



Source link

Previous articleBitcoin To The Rescue? Saylor Says EU Will Need BTC Amid Euro Woes
Next articleBitcoin Price Watch: Tight Range Signals a Major Move Incoming – Bitcoin.com News