Bash – Modify bash autocomplete rule to exclude particular file names

autocompletebash

I want to modify the existing vi bash autocompletion rule to exclude some filenames (for example, the config.log file). I tried using the FIGNORE environmental variable, but that didn't work. Using the complete builtin overrides the existing rule, rather than modifying it.

Is there any way to do this?

Best Answer

You can see the current configuration with

complete -p vi
complete -o bashdefault -o default -o filenames -o nospace -F _exp_ vi

and the definition of the used shell function with

type _exp_

There is a complete feature / option -X which allows to filter the results. The problem is that this applies only to to complete actions. It does not apply to the -o default results (I don't know about -o bashdefault).

You can change the compspec:

complete -d -f -o filenames -o nospace -F _exp_ -X '@(config.log|config.doc)' vi

Unfortulately

  1. you can have only one -X in the complete call (former ones are overwritten)

  2. you need shopt -s extglob for the above to work; otherwise you are limited to a single name or pattern ("*.log")