Here is a Bash Completion Tutorial so that a command line user can either use the existing Bash completion examples or create own Bash Script. Bash Completion Tutorial, to certain extent is a misnomer. We have discussed about Terminal and Shell. Better terminology will be Command line completion, which is a common feature of all the command line interpreters. The program actually automatically fills the partially typed commands after proper setup. There are ready made scripts as well as custom scripts can be used. Again the need of knowing to write shell scripts becomes apparent.
Bash Completion Tutorial : Basics First
Bash uses GNU Readline Library which features history and basic completion. One can run this command from command line (for any UNIX, OS X/Mac, Linux, Unix-like system) to know about it :
1 | curl http://cnswww.cns.cwru.edu/php/chet/readline/README > readline.txt && nano readline.txt |
The Debian bash package has a command line completion file located at /etc/bash_completion
which sets up some common support. We just need to execute it by running . /etc/bash_completion
command. If you type /ro
and press TAB key, it becomes /root
. You can create an Alioth account at https://alioth.debian.org
for own pull. You know about unix pipeline. If you run this command :
---
1 | bind -p | grep complete |
shows to which key the special feature is bound on the particular system. Bash Completion on deb system is a daemon, you can check /etc/bash_completion.d
. The reason to mention it is to possibly stopping the daemon before logging out from SSH session for deb based server. Helping a script kiddie, somehow logged in with additional auto-complete, is not good idea!
Keep in mind – Ubuntu or other deb based system, might not have the script by default.
The Readline library, manages the command line editing, and calls back to bash when tab is pressed, to enable completion. Bash itself has a feature named complete
to define a completion mechanism for individual commands. With complete «options» «command»
we can define the completion for some command, which is written here :
1 | http://info2html.sourceforge.net/cgi-bin/info2html-demo/info2html?%28bash.info.gz%29Programmable%2520Completion |
Bash Completion Tutorial : Homebrew + OSX + iTerm2 Setup
On Mac, Homebrew + OSX + iTerm2 Setup is commonly used. Oh-My-ZSH means ZSH, means Z Shell. You possibly have the plugin :
1 | https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/brew/_brew |
In other words, test it first with typing some commands and hitting Tab. When you are doing SSH to Server from iTerm2, thats not your local computer’s iTerm2. We get the echo
of the output, but machine
is the remote server. SSH
is different topic, you need to install the bash-completion
from brew install bash-completion
and this will be the output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ==> Patching patching file bash_completion ==> ./configure --prefix=/usr/local/Cellar/bash-completion/1.3 ==> make install ==> Caveats Add the following lines to your ~/.bash_profile: if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion fi Homebrew's own bash completion script has been installed to /usr/local/etc/bash_completion.d Bash completion has been installed to: /usr/local/etc/bash_completion.d |
The bug is, the lines should be :
1 2 3 | if [ -f $(brew --prefix)/etc/bash_completion ]; then . $(brew --prefix)/etc/bash_completion.d fi |
They forgotten the .d
part. You can load the environment by running source ~/.bash_profile
, it should not throw error.
Possibly most ask for Shell Integration :
1 | http://iterm2.com/shell_integration.html#/section/home |
Mike has an excellent article on yummy and useful Terminal :
1 | http://mikebuss.com/2014/02/02/a-beautiful-productive-terminal-experience/ |
You can easily waste a day reading his great writings. Unfortunately, none reads the good stuffs or probably Google is not at the level of understanding the good stuffs. The screenshot is from Mike Buss’s post.
Bash Completion Tutorial : Autocomplete in apt-get
In latest Ubuntu, the bash-complete
package is omitted. Script hichhandles completions for service is in /usr/share/bash-completion/bash_completion
and it looks for service names in /etc/rc.d/init.d
and /etc/init.d
, and output is systemctl list-units --full --all
. You should know about run level. You have to run this command – sudoedit /usr/share/bash-completion/completions/apt
and paste this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # Debian apt(8) completion -*- shell-script -*- _apt() { local cur prev words cword _init_completion || return local special i for (( i=0; i < ${#words[@]}-1; i++ )); do if [[ ${words[i]} == @(list|search|show|update|install|remove|upgrade|full-upgrade|edit-sources|dist-upgrade|purge) ]]; then special=${words[i]} fi done if [[ -n $special ]]; then case $special in remove|purge) if [[ -f /etc/debian_version ]]; then # Debian system COMPREPLY=( $( \ _xfunc dpkg _comp_dpkg_installed_packages $cur ) ) else # assume RPM based _xfunc rpm _rpm_installed_packages fi return 0 ;; *) COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \ 2> /dev/null ) ) return 0 ;; esac fi case $prev in -c|--config-file) _filedir return 0 ;; -t|--target-release|--default-release) COMPREPLY=( $( apt-cache policy | \ command grep "release.o=Debian,a=$cur" | \ sed -e "s/.*a=\(\w*\).*/\1/" | uniq 2> /dev/null) ) return 0 ;; esac if [[ "$cur" == -* ]]; then COMPREPLY=( $( compgen -W '-d -f -h -v -m -q -s -y -u -t -b -c -o --download-only --fix-broken --help --version --ignore-missing --fix-missing --no-download --quiet --simulate --just-print --dry-run --recon --no-act --yes --assume-yes --show-upgraded --only-source --compile --build --ignore-hold --target-release --no-upgrade --force-yes --print-uris --purge --reinstall --list-cleanup --default-release --trivial-only --no-remove --diff-only --no-install-recommends --tar-only --config-file --option --auto-remove' -- "$cur" ) ) else COMPREPLY=( $( compgen -W 'list search show update install remove upgrade full-upgrade edit-sources dist-upgrade purge' -- "$cur" ) ) fi return 0 } && complete -F _apt apt # ex: ts=4 sw=4 et filetype=sh |
As you can see, this script has some pre-defined words :
1 | if [[ ${words[i]} == @(list|search|show|update|install|remove|upgrade|full-upgrade|edit-sources|dist-upgrade|purge) ]] |
You can change the things. Reload the environment by running source ~/.bashrc
.
Bash Completion Tutorial : Own Scripts
The scripts inside /etc/bash_completion.d/
does the work for Debian. There are hundreds of scripts available for various advanced usage. Well, you can try to do a homework on which, on your Mac iTerm2, on typing ssh root followed by hitting Tab will return only few results which actually you SSH to. Hint is, the SSH complete function loads from here :
1 2 3 4 | cd /usr/share/zsh/ && ls cd <version-number> && ls cd functions nano _ssh |
If you do not edit it, it will load a huge list, just the output :
1 2 3 | ssh root@ # upon hitting [Tab] zsh: do you wish to see all 289 possibilities (289 lines)? [y/n] |
It is echoing all the known hosts and hundreds of stuffs. It also shows ZSH’s corrective capability – 289 is a big list, ZSH gave the option to think. Ok, another clue – this should be the format of the syntax needed to be added in some profile file :
1 2 3 4 5 | SSH_COMPLETE=( $(cut -f1 -d' ' ~/.ssh/your_hosts |\ tr ',' '\n' |\ sort -u |\ grep -e '[:alpha:]') ) complete -o default -W "${SSH_COMPLETE[*]}" ssh |
~/.ssh/your_hosts
will contain the list of your needed to SSH servers. Last clue – edit .zshrc
.