Tab Completion for Input - Images: Japanese
Hey there, everyone!
So, we've been getting hands-on with CentOS in CUI mode for a while now — and compared to a GUI where you can just point and click, the amount of typing involved in CUI can start to feel a bit exhausting.
A lot of commands involve symbols you'd rarely use outside of programming, so there's a fair chance that even touch-typing is a struggle for some of you.
Well, here's a feature that's going to make your life a whole lot easier — the 'Tab key'. Yep, that key sitting on the left side of your keyboard. In UNIX-like OSes, pressing the Tab key triggers 'auto-completion'. Let's explore how it works. This feature is also commonly called 'TAB completion' or just 'tab completion'.
Alright, open up CentOS and log in. The current directory doesn't matter for this one.
To demonstrate completion, let's introduce a new command: 'ifconfig'. This command lets you check information about network interfaces — things like connectors and ports.
The basic usage is straightforward — just type "ifconfig" and hit Enter:
[root@localhost ~]# ifconfig
But instead of typing the full thing, try typing just "ifco" and stopping there:
[root@localhost ~]# ifco
Now press the Tab key once:
[root@localhost ~]# ifconfig
The command got completed for you. Hit Enter and it runs.
In that example, one Tab press was enough to complete the command. But when there are multiple possible completions, a single Tab won't do it. Let's see what that looks like.
In a default CentOS install, "ifco..." only matches ifconfig. But "ifc..." also matches another command called ifcfg. If you type just "ifc" and press Tab once, this happens:
[root@localhost ~]# ifc
Nothing happens — if your sound is on, you might hear a beep. Completion doesn't trigger yet.
Press Tab a second time and you'll see:
[root@localhost ~]# ifc ifcfg ifconfig
The available candidates are displayed. The key point: press Tab twice to see the candidates.
From here, type a few more characters to narrow it down, then press Tab once more to complete it.
Let's try another example. There are many commands starting with "i" — if you type just "i" and press Tab twice:
[root@localhost ~]# i i386 initctl ipmaddr iconv insmod iptables iconvconfig insmod.static iptables-1.4.7 iconvconfig.x86_64 install iptables-multi id install-info iptables-multi-1.4.7 idn installkernel iptables-restore if ionice iptables-restore-1.4.7 ifcfg ip iptables-save ifconfig ip6tables iptables-save-1.4.7 ifdown ip6tables-1.4.7 iptables-xml ifenslave ip6tables-multi iptables-xml-1.4.7 ifup ip6tables-multi-1.4.7 iptunnel igawk ip6tables-restore iscsi-iname in ip6tables-restore-1.4.7 iscsiadm indxbib ip6tables-save iscsid info ip6tables-save-1.4.7 iscsistart infocmp ipcalc iscsiuio infokey ipcmk isosize infotocap ipcrm init ipcs
A big list appears. From there, type enough characters to identify your target and use Tab to complete it — same as before.
Commands slip out of memory even after years of use, especially the ones you don't reach for every day. But knowing this completion feature is there means a forgotten command name is never a real problem. The OS really comes through here.
And as you might have guessed — completion also gives you a way to check whether a command is installed on the current system. It's not a perfect check, but it gets you most of the way there.
Now let's talk about completing 'paths'. The completion we just saw was for command names, but it also works for path arguments — which is incredibly useful.
Let's try it with the 'ls' command we've been using. Type "ls" followed by a space:
[root@localhost ~]# ls
Then add "/r" after that:
[root@localhost ~]# ls /r
Press Tab and something neat happens:
[root@localhost ~]# ls /root/
It completed to '/root/' automatically. Very handy.
When there are multiple path candidates, pressing Tab twice shows them — same behavior as with commands. Here's what happens when you type "ls /s" and press Tab twice:
[root@localhost ~]# ls /s sbin/ selinux/ srv/ sys/
All the matching paths are shown. From there, add more characters and use Tab to complete.
The completion behavior described above is built into 'bash' — the default shell on CentOS. Depending on the shell you're using, completion can work differently.
For example, the author has used 'zsh' for many years. In 'zsh', when there are multiple completion candidates, pressing Tab cycles through them one by one — no need to type extra characters. It works like this:
Start in any directory. Type "vi" and press Tab once:
[@:~]$ vi VirtualBox vi view vifs vim vimdiff vimtutor vipw vis visudo
All commands starting with "vi" appear. Press Tab again:
[@:~]$ VirtualBox VirtualBox vi view vifs vim vimdiff vimtutor vipw vis visudo
The first candidate, VirtualBox, is now filled in. Press Tab once more:
[@:~]$ vi VirtualBox vi view vifs vim vimdiff vimtutor vipw vis visudo
Now vi is selected. Each Tab press cycles to the next candidate — no typing needed.
Path completion works the same way — just keep pressing Tab to cycle through directories and files.
[@:~]$ ls ~/ Applications/ Desktop/ Downloads/ Movies/ Pictures/ VirtualBox\ VMs/ Creative\ Cloud\ Files/ Documents/ Library/ Music/ Public/
[@:~]$ ls ~/Applications/ Applications/ Desktop/ Downloads/ Movies/ Pictures/ VirtualBox\ VMs/ Creative\ Cloud\ Files/ Documents/ Library/ Music/ Public/
[@:~]$ ls ~/Creative\ Cloud\ Files/ Applications/ Desktop/ Downloads/ Movies/ Pictures/ VirtualBox\ VMs/ Creative\ Cloud\ Files/ Documents/ Library/ Music/ Public/
One thing to note: while cycling through candidates, pressing any key other than Tab confirms the current selection. For example, pressing the left arrow key while "ls ~/Desktop/" is selected will lock in "~/Desktop" (without the trailing slash), and the next Tab press will then complete from there into the contents of that directory.
[@:~]$ ls ~/Desktop/ Applications/ Desktop/ Downloads/ Movies/ Pictures/ VirtualBox\ VMs/ Creative\ Cloud\ Files/ Documents/ Library/ Music/ Public/
[@:~]$ ls ~/Desktop/ test/ test.html
It sounds a bit tricky to explain in words, but once you actually try it, the behavior becomes obvious. Give it a try in different situations.
Shell is the software that acts as an intermediary — taking what you type and passing it along to the appropriate program. You might also hear it called a "command shell" or "command-line shell."
Technical references often describe it as something like "a set of programs that provide a user interface to the kernel" — but in plain terms, it's the software that reads your keyboard input and runs the corresponding program.
When you type 'ls' and press Enter, something behind the scenes recognizes the input and triggers the 'ls' program. That something is the shell. It's involved in just about every operation you perform on a UNIX-like system.
There are many different shells. The one you're almost certainly using right now is 'bash' — it's the default shell in the vast majority of modern UNIX-like systems.
The author has used 'zsh' for many years. It goes by various pronunciations — "Z shell", "zee shell", "zed shell" — but based on the official name, "Z shell" is the most accurate. It's a mature shell with a lot of useful features, so if you ever want to try something beyond bash, zsh is a solid first choice. Installation instructions are easy to find with a quick search.
One thing to be aware of: in most default configurations, tab completion is case-sensitive. So if you're trying to type 'ls' but start with an uppercase "L" and spam Tab, 'ls' won't appear.
[root@localhost ~]# L
If you want case-insensitive completion, it requires a configuration change — and the method depends on which shell you're using. Search for something like "bash case insensitive completion" for your specific shell.
So that's tab completion. Without it, CUI would be considerably harder to use. Commands alone might be manageable, but memorizing every path exactly is asking a lot. The people who built UNIX from the beginning really did think ahead by including this from the start.
In the next article, we'll look at creating directories. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.