Skip to content

Commit 59074b3

Browse files
committed
docs(README): reorganize Q&A for external completion settings
This patch reorganizes the descriptions for installing/overriding completion settings to solve multiple issues. * First, the systemwide settings and eagerly loaded settings were conflated. The systemwide settings are not necessarily eagerly loaded completions. * The paths obtained by "pkg-config" usually point to the directory under `/usr/share`, which are the places for system packages provided by the distributions. User startup files and systemwide local settings should not be placed under the tree `/usr/local` in typical distributions or with typical package managers. The user startup files should be installed into the user's directory, and systemwide local settings should be installed in `/usr/local/share`. * We also distinguish the new startup files from the traditional eagerly loaded files
1 parent 56907f2 commit 59074b3

File tree

1 file changed

+90
-47
lines changed

1 file changed

+90
-47
lines changed

README.md

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ tracing on in it before doing anything else there.
125125

126126
## FAQ
127127

128-
**Q. The bash completion code inhibits some commands from completing on
128+
**Q1. The bash completion code inhibits some commands from completing on
129129
files with extensions that are legitimate in my environment. Do I
130130
have to disable completion for that command in order to complete on
131131
the files that I need to?**
@@ -136,47 +136,85 @@ A. No. If needed just once in a while,
136136
circumvent any file type restrictions put in place by the bash
137137
completion code. If needed more regularly, see the next question:
138138

139-
**Q. How can I override a completion shipped by bash-completion?**
140-
141-
A. Install a local completion of your own appropriately for the desired
142-
command, and it will take precedence over the one shipped by us. See the
143-
next answer for details where to install it, if you are doing it on per user
144-
basis. If you want to do it system wide, you can install eagerly loaded
145-
files in `<startupdir>`, whose value in the current system can be retrieved
146-
by `pkg-config bash-completion --variable=startupdir`, and install a
147-
completion for the commands to override our completion for in them.
148-
149-
If you want to use bash's default completion instead of one of ours,
150-
something like this should work (where `$cmd` is the command to override
151-
completion for): `complete -o default -o bashdefault $cmd`
152-
153-
**Q. Where should I install my own local completions?**
154-
155-
A. Put them in the `completions` subdir of `$BASH_COMPLETION_USER_DIR`
156-
(defaults to `$XDG_DATA_HOME/bash-completion` or
157-
`~/.local/share/bash-completion` if `$XDG_DATA_HOME` is not set) to have
158-
them loaded automatically on demand when the respective command is being
159-
completed.
160-
See also the next question's answer for considerations for these
161-
files' names, they apply here as well. Alternatively, you can write
162-
them directly in `~/.bash_completion` which is loaded eagerly by
163-
our main script.
164-
165-
**Q. I author/maintain package X and would like to maintain my own
166-
completion code for this package. Where should I put it to be sure
167-
that interactive bash shells will find it and source it?**
168-
169-
A. [ Disclaimer: Here, how to make the completion code visible to
170-
bash-completion is explained. We do not require always making the
171-
completion code visible to bash-completion. In what condition the
172-
completion code is installed should be determined at the author/maintainers'
173-
own discretion. ]
139+
**Q2. How can I override a completion shipped by bash-completion or install a
140+
new completion for a user account?**
141+
142+
A. To override a completion for a specific command, you can simply install your
143+
own completion file for the command into an appropriate place. It will take
144+
precedence over the one shipped by us or the one installed systemwide. To
145+
install a completion on per user basis, put the completion file at
146+
`<userdir>/completions/<cmd>.bash`, where `<cmd>` is the command name and
147+
`<userdir>` is one of the user directories. The user directories are
148+
specified by the colon-separated `$BASH_COMPLETION_USER_DIR` (defaults to
149+
`$XDG_DATA_HOME/bash-completion` or `~/.local/share/bash-completion` if
150+
`$XDG_DATA_HOME` is not set). They will be loaded automatically on demand
151+
when the respective command is being completed.
152+
153+
> [!NOTE]
154+
> See also the answer to Q4 for considerations for those files' names, they
155+
> apply here as well.
156+
157+
Completion settings that should be loaded on initialization of
158+
bash-completion should be defined in a startup completion file. A startup
159+
completion file can be added in the directory `<userdir>/startup` (see the
160+
previous paragraph for `<userdir>`). A prefix of the form
161+
`[0-9][0-9][0-9]_` may be prepended to the filename of the startup files to
162+
control the loading order. To override a existing startup file installed
163+
systemwide or shipped by us, you can put your own version with the same
164+
filename in `<userdir>/startup`. In identifying the overridden startup
165+
file, the prefixes `[0-9][0-9][0-9]_` are ignored. If you want to disable a
166+
startup completion file, you can put an empty file.
167+
168+
You can also define eagerly loaded settings in the user file
169+
(`${BASH_COMPLETION_USER_FILE:-$HOME/.bash_completion}`). Instead of
170+
preparing separate files for specific commands, you may write completion
171+
settings for specific commands directly in the user file. When you want to
172+
modify the default completion setting set by `complete -D` (which is
173+
available in Bash >= 4.1), you can override it in the user file or in a user
174+
startup file. If you want to use bash's default completion instead of one
175+
of ours, in the user file or in a user startup script, you can define
176+
something like this:
177+
178+
```bash
179+
complete -o default -o bashdefault $cmd
180+
```
181+
182+
where `$cmd` is the command to override completion for.
183+
184+
**Q3. How can I override a completion shipped by bash-completion systemwide?**
185+
186+
A. Install a local completion appropriately for the desired command, and it
187+
will take precedence over the one shipped by us.
188+
189+
A completion file for a specific command `<cmd>` can be placed at
190+
`<localdir>/completions/<cmd>.bash`, where `<localdir>` is
191+
`/usr/local/share/bash-completion` if `XDG_DATA_DIRS` is not defined, or
192+
`<datadir>/bash-completion` where `<datadir>` is one of the directories
193+
listed in `XDG_DATA_DIRS`.
194+
195+
> [!NOTE]
196+
> See also the answer to Q4 for considerations for those files' names, they
197+
> apply here as well.
198+
199+
Similarly, systemwide startup files can be placed in the directory
200+
`<localdir>/startup`.
201+
202+
**Q4. I author/maintain a distribution package and would like to maintain my
203+
own completion code for this package. Where should I put it to be sure that
204+
interactive bash shells will find it and source it?**
205+
206+
A. > [!NOTE]
207+
> Here, how to make the completion code visible to bash-completion is
208+
> explained. We do not require always making the completion code visible to
209+
> bash-completion. In what condition the completion code is installed
210+
> should be determined at the author/maintainers' own discretion.
174211
175212
Install it in one of the directories pointed to by bash-completion's
176-
`pkgconfig` file variables. There are two alternatives:
213+
`pkgconfig` file variables.
177214

178-
- The recommended directory is `<completionsdir>`, which you can get with
179-
`pkg-config --variable=completionsdir bash-completion`. From this
215+
- The recommended directory for the completions of specific commands is
216+
`<completionsdir>`, which you can get with
217+
`pkg-config --variable=completionsdir bash-completion`. From this
180218
directory, completions are automatically loaded on demand based on invoked
181219
commands' names, so be sure to name your completion file accordingly, and
182220
to include (for example) symbolic links in case the file provides
@@ -188,6 +226,11 @@ A. [ Disclaimer: Here, how to make the completion code visible to
188226
--variable=helpersdir bash-completion`. The completion files in
189227
`<completionsdir>` can reference the helper script `<helpersdir>/<helper>`
190228
as `${BASH_SOURCE[0]%/*}/../helpers/<helper>`.
229+
- The directory for startup files is `<startupdir>`, whose value in the
230+
current system can be retrieved by
231+
`pkg-config bash-completion --variable=startupdir`. A typical value is
232+
`/usr/share/bash-completion/startup`. Typically, the startup file can be
233+
used to override the `complete -D` settings set by bash-completion.
191234
- The other directory, which is only present for backwards compatibility and
192235
is not recommended to use, is `<compatdir>` (get it with
193236
`pkg-config --variable=compatdir bash-completion`). From this
@@ -234,8 +277,7 @@ A. [ Disclaimer: Here, how to make the completion code visible to
234277
bash-completion is 2.12 or higher, the completion script can actually be
235278
installed to `$PREFIX/share/bash-completion/completions/` under the same
236279
installation prefix as the target program installed under `$PREFIX/bin/` or
237-
`$PREFIX/sbin/`. For the detailed search order, see also "Q. What is the
238-
search order for the completion file of each target command?" below.
280+
`$PREFIX/sbin/`. For the detailed search order, see also Q10 below.
239281

240282
Example for `Makefile.am`:
241283

@@ -250,7 +292,7 @@ A. [ Disclaimer: Here, how to make the completion code visible to
250292
install(FILES your-completion-file DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")
251293
```
252294

253-
**Q. When completing on a symlink to a directory, bash does not append
295+
**Q5. When completing on a symlink to a directory, bash does not append
254296
the trailing `/` and I have to hit <kbd>&lt;Tab></kbd> again.
255297
I don't like this.**
256298

@@ -263,7 +305,7 @@ A. This has nothing to do with `bash_completion`. It's the default for
263305
mark-symlinked-directories on` in your `/etc/inputrc` or
264306
`~/.inputrc` file.
265307

266-
**Q. Completion goes awry when I try to complete on something that contains
308+
**Q6. Completion goes awry when I try to complete on something that contains
267309
a colon.**
268310

269311
A. This is actually a 'feature' of bash. bash recognises a colon as
@@ -291,7 +333,7 @@ A. This is actually a 'feature' of bash. bash recognises a colon as
291333
Unfortunately, there's no way to turn this off. The only thing you
292334
can do is escape the colons with a backslash.
293335

294-
**Q. Why is `rpm` completion so slow with `-q`?**
336+
**Q7. Why is `rpm` completion so slow with `-q`?**
295337

296338
A. Probably because the database is being queried every time and this uses a
297339
lot of memory.
@@ -314,7 +356,7 @@ A. Probably because the database is being queried every time and this uses a
314356
unless it detects that the database has changed since the file was created,
315357
in which case it will still use the database to ensure accuracy.
316358

317-
**Q. bash-completion interferes with my `command_not_found_handle` function
359+
**Q8. bash-completion interferes with my `command_not_found_handle` function
318360
(or the other way around)!**
319361

320362
A. If your `command_not_found_handle` function is not intended to address
@@ -334,7 +376,7 @@ A. If your `command_not_found_handle` function is not intended to address
334376
> context. It is safer to test `COMP_POINT` as one does not need to care
335377
> about the differences between the set and non-empty states of variables.
336378
337-
**Q. Can tab completion be made even easier?**
379+
**Q9. Can tab completion be made even easier?**
338380

339381
A. The `readline(3)` library offers a few settings that can make tab
340382
completion easier (or at least different) to use.
@@ -363,7 +405,8 @@ A. The `readline(3)` library offers a few settings that can make tab
363405
This turns off the use of the internal pager when returning long
364406
completion lists.
365407

366-
**Q. What is the search order for the completion file of each target command?**
408+
**Q10. What is the search order for the completion file of each target
409+
command?**
367410

368411
A. The completion files of commands are looked up by the shell function
369412
`_comp_load`. Here, the search order in bash-completion >= 2.18 is

0 commit comments

Comments
 (0)