TIP TAB-completion

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

[edit] Bash Tab-Completion

By default Bash will complete filenames if you press the tab key. This is useful and saves typing but doesn't take into account context. For example the first argument to the chown command is a username not a file.

Following the steps below will give you enhanced completion, taking context into account such as this partial list of examples:

  • Commands which only take directories (eg cd get only those)
  • Document types (eg PDF readers complete to pdf files)
  • Usernames and groups are supplied where appropriate (eg chown and chgrp)
  • Process ids (eg kill), devices (eg mount), man pages (eg man) are supplied as appropriate
  • Command line options (eg type chown - and hit tab)
  • Command line options are taken into account. eg if you press tab after find -uid you get a list of userids and after find -fstype you get a list of file system types.
  • You can write your own completions (see /etc/bash_completion and /etc/bash_completion.d for how the existing ones are implemented)

[edit] Installation

First, edit /etc/make.conf and add the bash-completion USE flag. Every package on your system supporting this USE flag - not compiled with this USE flag, should now be recompiled:

# emerge -avuN world

Then, install bash-completion:

# emerge -av bash-completion gentoo-bashcomp

Now add the following line to /etc/bash/bashrc, before setting any alias (see bug #98627):

File: /etc/bash/bashrc
[[ -f /etc/profile.d/bash-completion ]] && source /etc/profile.d/bash-completion

Let the changes take effect:

# source /etc/bash/bashrc

Or exit the shell and login again.

[edit] Configuration

There are two tools for configuring bash-completion: eselect and bash-completion-config. Choose one of them and install it:

# emerge -av eselect

or

# emerge -av bash-completion-config

[edit] Portage Related Commands

By default, Bash tab-completion support for Portage related commands (emerge, equery, revdep-rebuild etc.) is globally enabled, but usually only the root user can use them. Unless other users are part of the portage group, enter the following commands:

  • if using eselect:
# eselect bashcomp disable --global gentoo
# eselect bashcomp enable gentoo
  • if using bash-completion-config:
# bash-completion-config --uninstall --global gentoo
# bash-completion-config --install gentoo

At last:

# source /etc/bash/bashrc

[edit] Other commands

Only Bash tab-completion support for the Portage-related commands is enabled by default, but as we previously said other tools (unrar, genlop etc.; here is the full list) support this nice feature as well.

If you want to enable Bash tab-completion for the genlop command, for example, just type:

  • if using eselect:
# eselect bashcomp enable genlop
  • if using bash-completion-config:
# bash-completion-config --install genlop 

Remember, for the changes to have an immediate effect, issue the following command:

# source /etc/bash/bashrc

[edit] Enabling All Completions

If you want to enable all of the installed completions you can run this command:

# for i in `ls --color=no /usr/share/bash-completion/`; do ln -s /usr/share/bash-completion/$i ~/.bash_completion.d/$i; done

Or, if you want to enable all installed completions globally, run this as root:

# for i in `ls --color=no /usr/share/bash-completion/`; do ln -s /usr/share/bash-completion/$i /etc/bash_completion.d/$i; done

[edit] Customizations

  • Activate Bash completion with one TAB key press instead of two TAB key presses.
File: ~/.inputrc
 set show-all-if-ambiguous On
$ bash -$-

If you are creating your ~/.inputrc file, you may find this breaks your Home and End keys. If you find this happens, you may like to add this:

File: ~/.inputrc
 "\e[1~": beginning-of-line
 "\e[4~": end-of-line 

[edit] Known Bugs

Bug: Problem at disabling a module in bash-completion
For details, see bug report #133765

[edit] Zsh Completion

If you want to enable Portage related commands completion, install zsh-completion:

# emerge -av zsh-completion

Now add the following lines to your ~/.zshrc:

File: ~/.zshrc
 autoload -U compinit promptinit
 compinit
 promptinit; prompt gentoo

Also, if you want to enable cache for the completions, add to your ~/.zshrc:

File: ~/.zshrc
zstyle ':completion::complete:*' use-cache 1


[edit] Testing

Let's check if everything went well. Without pressing the return key, type:

# emerge rkh

Now press the tab key. You should obtain:

# emerge rkhunter

Wonderful, isn't it? ;)

Personal tools
In other languages