Skip to content

Commit e18b0de

Browse files
citrusmunchmpilgrem
authored andcommitted
update docs for shell auto-completion (Bash, zsh, fish)
1 parent 3ca67ef commit e18b0de

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

doc/shell_autocompletion.md

+40-49
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,68 @@
33
# Shell auto-completion
44

55
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).
910

10-
=== "Bash"
11+
!!! info
1112

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:
1317

1418
~~~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)
2225

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
3029

31-
for arg in ${COMP_WORDS[@]}; do
32-
CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)
33-
done
30+
COMPREPLY=( $(stack "${CMDLINE[@]}") )
31+
}
3432

35-
COMPREPLY=( $(stack "${CMDLINE[@]}") )
36-
}
37-
~~~
33+
complete -o filenames -F _stack stack
34+
~~~
3835

39-
=== "Zsh"
36+
=== "Bash"
4037

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`).
4440

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+
~~~
5044

51-
Consequently, you must:
45+
You may need to `source` this.
5246

53-
1. launch `compinint`
54-
2. launch `bashcompinit`
55-
3. eval Stack's Bash completion script
47+
=== "Zsh"
5648

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`).
5851

5952
~~~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)
6554
~~~
6655

67-
!!! info
56+
You won't need to `source` this, but do update your `fpath`:
6857

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+
~~~
7162

72-
=== "Fish"
63+
=== "fish"
7364

7465
Add the output of the following command to your preferred completions file
7566
(e.g. `~/.config/fish/completions/stack.fish`).
7667

7768
~~~fish
78-
stack --fish-completion-script stack
69+
stack --fish-completion-script $(which stack)
7970
~~~

0 commit comments

Comments
 (0)