bashcomplete.rst
This file
Path
docs/bashcomplete.rst
163 lines · 4.7 KiB
Last changed by
clean up completion docsDavid Lordbefore adoption6bf782e
History of this fileTake it
1Shell Completion2================34.. versionadded:: 2.056Click can provide tab completion for commands, options, and choice7values. Bash, Zsh, and Fish are supported89Completion is only available if a script is installed and invoked10through an entry point, not through the ``python`` command. See11:ref:`setuptools-integration`.121314What it Completes15-----------------1617Generally, the shell completion support will complete commands,18options, and any option or argument values where the type is19:class:`click.Choice`. Options are only listed if at least a dash has20been entered.2122.. code-block:: text2324 $ repo <TAB><TAB>25 clone commit copy delete setuser26 $ repo clone -<TAB><TAB>27 --deep --help --rev --shallow -r2829Custom completions can be provided for argument and option values by30providing an ``autocompletion`` function that returns a list of strings.31This is useful when the suggestions need to be dynamically generated32completion time. The callback function will be passed 3 keyword33arguments:3435- ``ctx`` - The current command context.36- ``args`` - The list of arguments passed in.37- ``incomplete`` - The partial word that is being completed. May38 be an empty string if no characters have been entered yet.3940Here is an example of using a callback function to generate dynamic41suggestions:4243.. code-block:: python4445 import os4647 def get_env_vars(ctx, args, incomplete):48 return [k for k in os.environ.keys() if incomplete in k]4950 @click.command()51 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)52 def cmd1(envvar):53 click.echo('Environment variable: %s' % envvar)54 click.echo('Value: %s' % os.environ[envvar])555657Completion help strings58-----------------------5960ZSH and fish support showing documentation strings for completions.61These are taken from the help parameters of options and subcommands. For62dynamically generated completions a help string can be provided by63returning a tuple instead of a string. The first element of the tuple is64the completion and the second is the help string to display.6566Here is an example of using a callback function to generate dynamic67suggestions with help strings:6869.. code-block:: python7071 import os7273 def get_colors(ctx, args, incomplete):74 colors = [('red', 'a warm color'),75 ('blue', 'a cool color'),76 ('green', 'the other starter color')]77 return [c for c in colors if incomplete in c[0]]7879 @click.command()80 @click.argument("color", type=click.STRING, autocompletion=get_colors)81 def cmd1(color):82 click.echo('Chosen color is %s' % color)838485Activation86----------8788In order to activate shell completion, you need to inform your shell89that completion is available for your script. Any Click application90automatically provides support for that. If the program is executed with91a special ``_<PROG_NAME>_COMPLETE`` variable, the completion mechanism92is triggered instead of the normal command. ``<PROG_NAME>`` is the93executable name in uppercase with dashes replaced by underscores.9495If your tool is called ``foo-bar``, then the variable is called96``_FOO_BAR_COMPLETE``. By exporting it with the ``source_{shell}``97value it will output the activation script to evaluate.9899Here are examples for a ``foo-bar`` script.100101For Bash, add this to ``~/.bashrc``:102103.. code-block:: text104105 eval "$(_FOO_BAR_COMPLETE=source_bash foo-bar)"106107For Zsh, add this to ``~/.zshrc``:108109.. code-block:: text110111 eval "$(_FOO_BAR_COMPLETE=source_zsh foo-bar)"112113For Fish, add this to ``~/.config/fish/completions/foo-bar.fish``:114115.. code-block:: text116117 eval (env _FOO_BAR_COMPLETE=source_fish foo-bar)118119Open a new shell to enable completion. Or run the ``eval`` command120directly in your current shell to enable it temporarily.121122123Activation Script124-----------------125126The above ``eval`` examples will invoke your application every time a127shell is started. This may slow down shell startup time significantly.128129Alternatively, export the generated completion code as a static script130to be executed. You can ship this file with your builds; tools like Git131do this. At least Zsh will also cache the results of completion files,132but not ``eval`` scripts.133134For Bash:135136.. code-block:: text137138 _FOO_BAR_COMPLETE=source_bash foo-bar > foo-bar-complete.sh139140For Zsh:141142.. code-block:: text143144 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh145146For Fish:147148.. code-block:: text149150 _FOO_BAR_COMPLETE=source_zsh foo-bar > foo-bar-complete.sh151152In ``.bashrc`` or ``.zshrc``, source the script instead of the ``eval``153command:154155.. code-block:: text156157 . /path/to/foo-bar-complete.sh158159For Fish, add the file to the completions directory:160161.. code-block:: text162163 _FOO_BAR_COMPLETE=source_fish foo-bar > ~/.config/fish/completions/foo-bar-complete.fish