Skip to content

Commit 3b434ac

Browse files
committed
Tidy up shell auto-completion
1 parent 71688d8 commit 3b434ac

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

doc/shell_autocompletion.md

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,70 @@
11
<div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div>
22

3-
# Shell Auto-completion
3+
# Shell auto-completion
44

5-
Note: if you installed a package for you Linux distribution, the bash
6-
completion file was automatically installed (you may need the `bash-completion`
7-
package to have it take effect).
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
8+
[#823](https://github.com/commercialhaskell/stack/issues/832).
89

9-
The following adds support for shell tab completion for standard Stack
10-
arguments, although completion for filenames and executables etc. within Stack
11-
is still lacking (see [issue
12-
823](https://github.com/commercialhaskell/stack/issues/832)).
10+
=== "Bash"
1311

14-
## for bash users
12+
Issue the following command or add it to your `~/.bashrc` file:
1513

16-
You need to run following command:
14+
~~~bash
15+
eval "$(stack --bash-completion-script stack)"
16+
~~~
1717

18-
~~~bash
19-
eval "$(stack --bash-completion-script stack)"
20-
~~~
18+
!!! info
2119

22-
You can also add it to your `.bashrc` file if you want.
20+
Stack's hidden option `--bash-completion-script <stack_executable_name>`
21+
outputs a command that can be evaluated by Bash. For example:
2322

24-
## for ZSH users
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)
2530

26-
documentation says:
27-
> Zsh can handle bash completions functions. The latest development version of
28-
> zsh has a function bashcompinit, that when run will allow zsh to read bash
29-
> completion specifications and functions. This is documented in the zshcompsys
30-
> man page. To use it all **you need to do is run bashcompinit at any time
31-
> after compinit**. It will define complete and compgen functions corresponding
32-
> to the bash builtins.
31+
for arg in ${COMP_WORDS[@]}; do
32+
CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)
33+
done
3334

34-
You must so:
35-
1. launch compinint
36-
2. launch bashcompinit
37-
3. eval stack bash completion script
35+
COMPREPLY=( $(stack "${CMDLINE[@]}") )
36+
}
37+
~~~
3838

39-
~~~shell
40-
autoload -U +X compinit && compinit
41-
autoload -U +X bashcompinit && bashcompinit
42-
eval "$(stack --bash-completion-script stack)"
43-
~~~
39+
=== Zsh
4440

45-
:information_source: If you already have quite a large zshrc, or if you use
46-
oh-my-zsh, **compinit** will probably already be loaded. If you have a blank
47-
zsh config, all of the 3 lines above are necessary.
41+
The Zsh
42+
[manual](https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Completion-System)
43+
explains:
4844

49-
:gem: tip: instead of running those 3 lines from your shell every time you want
50-
to use stack, you can add those 3 lines in your $HOME/.zshrc file
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.
50+
51+
Consequently, you must:
52+
53+
1. launch `compinint`
54+
2. launch `bashcompinit`
55+
3. eval Stack's Bash completion script
56+
57+
Issue the following commands or that them to your `~/.zshrc` file:
58+
59+
~~~zsh
60+
autoload -U +X compinit
61+
compinit
62+
autoload -U +X bashcompinit
63+
bashcompinit
64+
eval "$(stack --bash-completion-script stack)"
65+
~~~
66+
67+
!!! info
68+
69+
If you already have quite a large `.zshrc` file, or if you use
70+
`oh-my-zsh`, `compinit` will probably already be loaded.

0 commit comments

Comments
 (0)