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