Damien Martin-Guillerez | 21e3a6b | 2015-04-10 20:24:32 +0000 | [diff] [blame] | 1 | --- |
| 2 | layout: posts |
| 3 | title: Support for Bash Shell Completion |
| 4 | --- |
| 5 | |
| 6 | We just pushed a support for [shell completion in the Bourne-Again |
| 7 | Shell](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html). |
| 8 | It eases the use of Bazel by expanding its commands and the targets to build. |
| 9 | |
| 10 | To use this new functionality, build the `//scripts:bash_completion` target |
| 11 | from the Bazel repository: |
| 12 | ``` |
| 13 | bazel build //scripts:bash_completion |
| 14 | ``` |
| 15 | |
| 16 | This will create a `bazel-bin/scripts/bazel-complete.bash` completion script. |
| 17 | You can copy then copy this script to your completion directory |
| 18 | (`/etc/bash_completion.d` in Ubuntu). If you don't want to install it globally |
| 19 | or don't have such a directory, simply add the following line to your |
| 20 | `~/.bashrc` or `~/.bash_profile` (the latter is the recommended for OS X): |
| 21 | ``` |
| 22 | source /path/to/bazel/bazel-bin/scripts/bazel-complete.bash |
| 23 | ``` |
| 24 | |
| 25 | After that you should be able to type the tab key after the `bazel` |
| 26 | command in your shell and see the list of possible completions. |
| 27 | |
| 28 | If you are interested in supporting other shells, the script is made up |
| 29 | of two parts: |
| 30 | |
Kristina Chodorow | 2b1763a | 2015-09-01 09:15:54 +0000 | [diff] [blame^] | 31 | 1. [`scripts/bazel-complete-header.bash`](https://github.com/bazelbuild/bazel/blob/master/scripts/bazel-complete-template.bash) |
Damien Martin-Guillerez | 21e3a6b | 2015-04-10 20:24:32 +0000 | [diff] [blame] | 32 | is the completion logic. |
| 33 | 2. `bazel info completion` dumps the list of commands of Bazel, their options |
| 34 | and for commands and options that expect a value, a description of what is |
| 35 | expected. This description is either: |
| 36 | |
| 37 | * an enum of values enclosed into brackets, e.g., `{a,b,c}`; |
| 38 | * a type description, currently one of: |
| 39 | |
| 40 | * `label`, `label-bin`, `label-test`, `label-package` for |
| 41 | a Bazel label for, respectively, a target, a runnable target, |
| 42 | a test, and a package, |
| 43 | * `path` for a filesystem path, |
| 44 | * `info-key` for one of the information keys as listed by `bazel info`; |
| 45 | |
| 46 | * a combination of possible values using `|` as a separator, e.g, |
| 47 | `path|{or,an,enum}'`. |
| 48 | |
| 49 | Let us know if you have any questions or issues on the |
| 50 | [mailing list](https://groups.google.com/forum/#!forum/bazel-discuss) or |
Kristina Chodorow | 2b1763a | 2015-09-01 09:15:54 +0000 | [diff] [blame^] | 51 | [GitHub](https://github.com/bazelbuild/bazel). |