×

Bash $PATH filtering – https://purpleidea.com/

Bash $PATH filtering – https://purpleidea.com/


Bash $PATH filtering
How to filter things out of bash completion


As most modern GNU+Linux distro users already know, you get a lot of tools
included for free! Many of these may clutter up your $PATH and make bash tab
completion more difficult than it has to be. Here’s a way to improve this!

A mess:

Here’s what I see when I tab-complete cd<TAB>:

james@computer:~$ cd
cd                 cddb_query         cd-info            cd-read
cd-convert         cd-drive           cd-it8             cdrecord
cd-create-profile  cd-fix-profile     cdmkdir            cdtmpmkdir
cdda-player        cd-iccdump         cd-paranoia

I genuinely only use three of those commands.

let’s make some tab completions more complete

Bash to the rescue:

It turns out the bash authors already thought of this. There’s an EXECIGNORE
variable that you can set, and it will filter things out. Look what happens when
you set it:

james@computer:~$ EXECIGNORE='*/py*:*/cd*'
james@computer:~$ cd
cd          cdmkdir     cdtmpmkdir

I rarely use any python things (and this is only an example) and I actually
don’t need any of those cd* commands in my $PATH. Pass in a colon
separated list of patterns to ignore, and magically things just work!

If you include this variable in your ~/.bashrc then it will load automatically
every time you open a new terminal.

Which cd commands are those?

If you noticed the cdmkdir and cdtmpmkdir commands and wondered why they
didn’t disappear, that’s because they’re mine!

james@computer:~$ which cdmkdir
cdmkdir ()
{
    mkdir "$@" && cd "${@: -1}"
}
james@computer:~$ which cdtmpmkdir
cdtmpmkdir ()
{
    cd `mktemp --tmpdir -d tmp.XXX`
}

Specifics:

From the manual:


A colon-separated list of shell patterns (see Pattern Matching)
defining the list of filenames to be ignored by command search using PATH.
Files whose full pathnames match one of these patterns are not considered
executable files for the purposes of completion and command execution via PATH
lookup. This does not affect the behavior of the [, test, and [[ commands.
Full pathnames in the command hash table are not subject to EXECIGNORE. Use
this variable to ignore shared library files that have the executable bit set,
but are not executable files. The pattern matching honors the setting of the
extglob shell option.

Conclusion:

I hope that makes your bash tab completion filtering easier!

Happy Hacking,

James

You can follow James on Mastodon for more frequent updates and other random thoughts.

You can follow James on Twitter for more frequent updates and other random thoughts.

You can support James on GitHub if you’d like to help sustain this kind of content.

You can support James on Patreon if you’d like to help sustain this kind of content.





Source link