3

I've ignored __pycache__ in my git repos and in vi's wildmenu. The last place these directories pollute my workflow is when recursively grepping through project files and when using tab auto-completion on the command line.

Is there a way to configure command-line tools like grep and bash-completion to universally ignore directories?

2

With bash you can make file globbing drop __pycache__, by setting:

$ export GLOBIGNORE=__pycache__

Now if you issue:

$ ls __*
ls: cannot access __*: No such file or directory

Also with bash You can use the FIGNORE environment variable to skip file suffixes. It can be used to somewhat filter the directory...

$ mkdir __pycache__
$ export FIGNORE=__pycache__

Then when issuing:

$ ls __tab

will complete to

$ ls __pycache__

However, if it is prefixed (such as using a path):

$ ls ./__tab

it will not complete.

You can alias grep to exlude the __pycache__ directories:

alias grep='grep --exclude-dir="__pycache__"'

You could also use Madhavan Kumar's answer to change the completion of grep and the other commands you want to filter, but don't forget they will have to be defined in a rc file and sourced after sourcing bash_completion, to apply your overrides.

FIGNORE
    A colon-separated list of suffixes to ignore when performing filename 
completion (see READLINE below). A filename whose suffix matches one of 
the entries in FIGNORE is excluded from the list of matched filenames. A 
sample value is ".o:~". 

GLOBIGNORE
    A colon-separated list of patterns defining the set of filenames to
    be ignored by pathname expansion. If a filename matched by a pathname 
    expansion pattern also matches one of the patterns in GLOBIGNORE, it is 
    removed from the list of matches. 
3

You can change the compsec for these commands, to ignore them.

Say, for example, to get the existing compsec, use

complete -p grep

returns,

 complete -F _longopt grep

Now add the exclude option to this command as,

complete -F _longopt -X '@(__pycache__|more__patterns)' grep

You will not see those filenames on tab press

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.