10 tips on command line autocompletion in Linux

  
 

On Linux systems, when you type a command and press TAB twice, all available commands starting with the characters you type are listed. This is not new, you may already know. This feature is called command line completion bash completion. By default, the bash command line can automatically complete file or directory names. However, we can enhance the bash command completion function and bring it to a new level with the complete command.

This tutorial shows how we can apply auto-completion to options or command line arguments using programmable command completion.

Example: After you enter the write command, if you press the TAB button twice, the auto-completion feature provides a list of users who can perform write operations. $ write [TAB][TAB]bala rajjason randyjohn ritumayla thomasnisha www-data

In the following example, the available hostname can be displayed for the telnet command: $ telnet [TAB][TAB]localhost dev-db Fileserver

To make the programmable command completion function work on your terminal, you only need to execute /etc/bash_completion as follows: # . /etc/bash_completion

You can also cancel The following comment in /etc/bash.bashrc (from Ubuntu Linux 13.04 system), so you don't need to execute the above command: ### enable bash completion in interactive shellsif ! shopt -oq posix; thenif [ -f /usr/share/bash-completion/bash_completion ]; then. /usr/share/bash-completion/bash_completionelif [ -f /etc/bash_completion ]; then. /etc/bash_completionfifi

If you don't find these The code, did not find the /etc/bash_completion file, then you only need to install the bash_completion package by using the apt-get command.

1. View existing command line completions

After enabling the programmable command line completion function, there are already some defined command completion functions. The complete command is used to define command line completion.

To see the existing command line completion, use the complete command as follows: complete -p

Copyright © Windows knowledge All Rights Reserved