|
2 | 2 |
|
3 | 3 | # Shell auto-completion |
4 | 4 |
|
5 | | -The following adds support for the tab completion of standard Stack arguments in |
6 | | -the Bash shell or the Z shell (Zsh). Completion of file names and executables |
7 | | -within Stack is still lacking. For further information, see issue |
| 5 | +The following adds support for the tab completion of standard Stack arguments to |
| 6 | +the following shell programs: Bash, Zsh (the Z shell) and fish. Completion of |
| 7 | +file names and executables within Stack is still lacking. For further |
| 8 | +information, see issue |
8 | 9 | [#823](https://github.com/commercialhaskell/stack/issues/832). |
9 | 10 |
|
10 | | -=== "Bash" |
| 11 | +!!! info |
11 | 12 |
|
12 | | - Issue the following command or add it to your `~/.bashrc` file: |
| 13 | + Stack's completion library provides |
| 14 | + [hidden options](https://github.com/pcapriotti/optparse-applicative#bash-zsh-and-fish-completions) |
| 15 | + for Bash, Zsh, and fish which output commands used for shell |
| 16 | + auto-completion. For example: |
13 | 17 |
|
14 | 18 | ~~~bash |
15 | | - eval "$(stack --bash-completion-script stack)" |
16 | | - ~~~ |
| 19 | + $ stack --bash-completion-script stack |
| 20 | + _stack() |
| 21 | + { |
| 22 | + local CMDLINE |
| 23 | + local IFS=$'\n' |
| 24 | + CMDLINE=(--bash-completion-index $COMP_CWORD) |
17 | 25 |
|
18 | | - !!! info |
| 26 | + for arg in ${COMP_WORDS[@]}; do |
| 27 | + CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg) |
| 28 | + done |
19 | 29 |
|
20 | | - Stack's hidden option `--bash-completion-script <stack_executable_name>` |
21 | | - outputs a command that can be evaluated by Bash. For example: |
| 30 | + COMPREPLY=( $(stack "${CMDLINE[@]}") ) |
| 31 | + } |
22 | 32 |
|
23 | | - ~~~text |
24 | | - stack --bash-completion-script stack |
25 | | - _stack.exe() |
26 | | - { |
27 | | - local CMDLINE |
28 | | - local IFS=$'\n' |
29 | | - CMDLINE=(--bash-completion-index $COMP_CWORD) |
| 33 | + complete -o filenames -F _stack stack |
| 34 | + ~~~ |
30 | 35 |
|
31 | | - for arg in ${COMP_WORDS[@]}; do |
32 | | - CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg) |
33 | | - done |
| 36 | +=== "Bash" |
34 | 37 |
|
35 | | - COMPREPLY=( $(stack "${CMDLINE[@]}") ) |
36 | | - } |
37 | | - ~~~ |
| 38 | + Add the output of the following command to your preferred completions file |
| 39 | + (e.g. `~/.config/bash_completions.d/stack`). |
38 | 40 |
|
39 | | -=== "Zsh" |
| 41 | + ~~~bash |
| 42 | + stack --bash-completion-script $(which stack) |
| 43 | + ~~~ |
40 | 44 |
|
41 | | - The Zsh |
42 | | - [manual](https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-System) |
43 | | - explains: |
| 45 | + You may need to `source` this. |
44 | 46 |
|
45 | | - > The function `bashcompinit` provides compatibility with bash’s |
46 | | - programmable completion system. When run it will define the functions, |
47 | | - `compgen` and `complete` which correspond to the bash builtins with the same |
48 | | - names. It will then be possible to use completion specifications and |
49 | | - functions written for bash. |
| 47 | +=== "Zsh" |
50 | 48 |
|
51 | | - Consequently, you must: |
| 49 | + Add the output of the following command to your preferred completions file |
| 50 | + (e.g. `~/.config/zsh/completions/_stack`). |
52 | 51 |
|
53 | | - 1. launch `compinint` |
54 | | - 2. launch `bashcompinit` |
55 | | - 3. eval Stack's Bash completion script |
| 52 | + ~~~zsh |
| 53 | + stack --zsh-completion-script $(which stack) |
| 54 | + ~~~ |
56 | 55 |
|
57 | | - Issue the following commands or that them to your `~/.zshrc` file: |
| 56 | + You won't need to `source` this, but do update your `fpath`: |
58 | 57 |
|
59 | 58 | ~~~zsh |
60 | | - autoload -U +X compinit |
61 | | - compinit |
62 | | - autoload -U +X bashcompinit |
63 | | - bashcompinit |
64 | | - eval "$(stack --bash-completion-script stack)" |
| 59 | + fpath=($HOME/.config/zsh/completions $fpath) |
| 60 | + autoload -U compinit && compinit |
65 | 61 | ~~~ |
66 | 62 |
|
67 | | - !!! info |
| 63 | +=== "fish" |
| 64 | + |
| 65 | + Add the output of the following command to your preferred completions file |
| 66 | + (e.g. `~/.config/fish/completions/stack.fish`). |
68 | 67 |
|
69 | | - If you already have quite a large `.zshrc` file, or if you use |
70 | | - `oh-my-zsh`, `compinit` will probably already be loaded. |
| 68 | + ~~~fish |
| 69 | + stack --fish-completion-script $(which stack) |
| 70 | + ~~~ |
0 commit comments