Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tab Completion

Hash completes commands, files and directories, and knows what many commands expect as arguments: variable names after $, directories after cd, aliases after unalias, git subcommands and branches, make targets, manual pages. The complete builtin adds completions for any command.

Basic Usage

Command Completion

Press TAB after typing part of a command:

#> ec<TAB>
#> echo     # Completed!

#> gi<TAB>
#> git      # Completed from PATH

File/Directory Completion

#> cat test<TAB>
#> cat testfile.txt    # Completed!

#> cd Doc<TAB>
#> cd Documents/       # Directory with trailing /

Multiple Matches

First TAB - Complete common prefix:

#> cat test<TAB>
#> cat test            # Common prefix

Second TAB - Show all matches:

#> cat test<TAB><TAB>
testfile.txt  test.md  test_data.csv
#> cat test

What Gets Completed

First Word (Command)

Completes from:

  • Built-in commands (cd, exit, alias, etc.)
  • Aliases from .hashrc
  • Shell functions
  • Executables in PATH, scanned once and rescanned when PATH changes or after 15 seconds
#> g<TAB><TAB>
ga    gc    gd    git    gl    gp    gs

Subsequent Words

By default, files and directories: relative and absolute paths, with tilde expansion. A name typed without a directory completes to a bare name (afile, not ./afile). Several commands get something better suited:

CommandCompletes
cd, pushd, rmdirDirectories only
unaliasAlias names
unsetVariable names; function names after -f
export, readonly, localVariable names
command, exec, builtin, type, which, sudo, doas, nohup, nice, time, env, xargs, watchA command for the first operand, then files
gitSubcommands; branches, tags and remotes for checkout, switch, merge, rebase, log, diff, push, pull and friends; files for add, rm, restore
make, gmakeTargets of GNUmakefile, makefile or Makefile in the current directory (-f and -C complete files and directories)
manPage names from MANPATH or manpath

Options (words starting with -) are not completed.

Variables

After $ or ${, shell variables and the environment:

#> echo $HO<TAB>
#> echo $HOME
#> echo ${HO<TAB>
#> echo ${HOME}

Users

~ followed by the start of a login name completes to ~name/:

#> ls ~ro<TAB>
#> ls ~root/
#> cat /<TAB>
bin/  boot/  dev/  etc/  home/  ...

#> cd ~/.c<TAB>
#> cd ~/.config/

Programmable Completion

complete records how a command’s arguments are completed, in the spirit of bash’s builtin but without arrays:

complete -W "start stop restart status" myservice
complete -F _mytool_complete mytool
complete -d cdto                 # directories only
complete -c sudo                 # commands (sudo already has this built in)
complete -o default -W "init build" tool   # files when no word matches
OptionMeaning
-W wordlistThe words in wordlist (separated by blanks) that start with the current word
-F functionRun function; every line it prints that starts with the current word is a candidate
-a -b -c -d -f -u -vAliases, builtins, commands, directories, files, users, variables
-o defaultFiles when nothing else matched
-o dirnamesDirectories when nothing else matched
-p [name...]Print the specifications, as lines that can be typed back
-r [name...]Remove them (all of them without names)

A -F function runs with $1 set to the command, $2 to the word being completed and $3 to the word before it, and with COMP_LINE, COMP_POINT and COMP_CWORD set to the whole line, the cursor position and the index of the word. It prints candidates, one per line; hash keeps those that start with $2, so the function may print its whole vocabulary or filter it itself:

_mytool_complete() {
    case $3 in
        --format) echo json; echo yaml; echo table ;;
        *)        mytool --list-commands ;;
    esac
}
complete -F _mytool_complete mytool

Put complete lines in ~/.hashrc to have them every session.

Features

Directory Handling

Directories get trailing /:

#> cd Doc<TAB>
#> cd Documents/

Tilde Expansion

#> cat ~/.<TAB>
~/.bashrc  ~/.config/  ~/.hashrc  ...

Path Completion

#> ls ~/projects/hash/src/m<TAB>
#> ls ~/projects/hash/src/main.c

Case Sensitivity

Completion is case-sensitive:

#> cat Test<TAB>   # Won't match "test.txt"
#> cat test<TAB>   # Matches "test.txt"

Behavior

Single Match

  • Completes full name
  • Adds space after (ready for next arg)
  • Directories get /

Multiple Matches

First TAB: Complete common prefix Second TAB: Show all matches in columns

No Matches

Bell/beep sound, line unchanged.

After Operators

Completion works after command chaining:

#> ls && ca<TAB>
#> ls && cat

#> cd /tmp ; pw<TAB>
#> cd /tmp ; pwd

Tips & Tricks

Quick Directory Navigation

#> cd /u/l/b<TAB>
#> cd /usr/local/bin/

Exploring Directories

#> ls /etc/<TAB><TAB>
# Shows all files in /etc/

Command Discovery

#> git<TAB><TAB>
git  git-shell  gitk  ...

Limitations

  • Options (--long, -x) are not completed; complete -W can supply them for a command.
  • git subcommand detection does not know which options take an argument (git -C dir <TAB> sees dir as the subcommand).
  • Candidates are plain words: no quoting of names with spaces.

Comparison with Bash

FeatureHashBash
Command completionYesYes
File/directory completionYesYes
Alias completionYesYes
Path completionYesYes
Double-TAB shows matchesYesYes
Variable completionYesYes
User completion (~name)YesYes
Directories after cdYesWith bash-completion
git, make, manYesWith bash-completion
Programmable completioncomplete -W, -F, actionsYes (with compgen, arrays)