我可以在 bash-completion 和 grep 中忽略 __pycache__ 目录吗?

drs*_*drs 4 bash autocomplete

__pycache__我在我的git仓库和viWildmenu中忽略了。这些目录污染我的工作流程的最后一个地方是在递归地 grep 项目文件时以及tab在命令行上使用自动完成功能时。

有没有办法配置命令行工具,例如grepbash-completion来普遍忽略目录?

Dra*_*oan 5

bash您可以通过设置来使文件通配丢弃__pycache__

$ export GLOBIGNORE=__pycache__
Run Code Online (Sandbox Code Playgroud)

现在如果你发出:

$ ls __*
ls: cannot access __*: No such file or directory
Run Code Online (Sandbox Code Playgroud)

bash可以使用FIGNORE环境变量来跳过文件后缀。它可以用来稍微过滤目录......

$ mkdir __pycache__
$ export FIGNORE=__pycache__
Run Code Online (Sandbox Code Playgroud)

然后发出时:

$ ls __tab

将完成到

$ ls __pycache__

但是,如果它带有前缀(例如使用路径):

$ ls ./__tab

它不会完成。

您可以使用别名grep来排除__pycache__目录:

alias grep='grep --exclude-dir="__pycache__"'
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Madhavan Kumar 的答案来更改您想要过滤的命令的完成度grep和其他命令,但不要忘记它们必须在 rc 文件中定义并在 source 之后获取bash_completion,以应用您的覆盖。

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. 
Run Code Online (Sandbox Code Playgroud)