linux - 参数后文件名的 Bash Tab 补全

标签 linux bash autocomplete centos7

我最近从 Scientific Linux 6 迁移到 CentOS 7,但在新操作系统中遇到 bash 选项卡补全问题。

软件版本

$ cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)

$ uname -r
3.10.0-229.14.1.el7.x86_64

$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

我有一个名为 ./run_prog.sh 的 bash 脚本(可执行文件),它使用 -c 选项(或 long --config=)在命令行上获取配置文件。

完整命令示例:

./run_prog.sh -c=./config/test-new-feature.conf
## or
./run_prog.sh --config=./config/test-new-feature.conf

在以前的 bash 版本中,我可以在 -c= 结构之后使用 Tab 键来完成目录和文件名。

完成预期制表符的示例(它在 SL6 中的工作原理):

./run_prog.sh -c=./conf[TAB]
 ## completes to
./run_prog.sh -c=./config/
 ## then type
./run_prog.sh -c=./config/test-n[TAB]
 ## completes to
./run_prog.sh -c=./config/test-new-feature.conf

CentOS 7 中新版本的 bash 将不会补全 -c= short 选项后的任何文件名。

CentOS 7 中 Tab 键补全损坏的示例:

./run_prog.sh -c=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh -c=./conf

但是,如果我用空格分隔 -c,文件名补全会按预期工作。

使用空格完成制表符的示例:

./run_prog.sh -c ./conf[TAB]
 ## completes to
./run_prog.sh -c ./config/
 ## then type
./run_prog.sh -c ./config/test-n[TAB]
 ## completes to
./run_prog.sh -c ./config/test-new-feature.conf

问题

我怎样才能让新版本的 bash 像旧版本的 bash 一样使用 Tab 键完成文件名?

编辑

此脚本有一个用于短 -c 选项的长版本,即 --config。长版本也不起作用。

./run_prog.sh --config=./conf[TAB]
 ## doesn't complete anything
./run_prog.sh --config=./conf

这让我觉得 bash 对选项开关 (-c) 和选项值之间缺少空格感到困惑。

我的旧 bash-completion 目录

$ ls -1 /media/old_hd/etc/bash_completion.d/
bzr
createrepo.bash
dkms
fcoeadm
fcoemon
gdbus-bash-completion.sh
git
gvfs-bash-completion.sh
lldpad
lldptool
perf
phoronix-test-suite
pk-completion.bash
rpmdevtools.bash-completion
subversion
yum.bash
yum-utils.bash

我的新 bash_completion 目录

$ ls -1 /etc/bash_completion.d/
createrepo
dkms
fcoeadm
fcoemon
genpkgmetadata.py
git
lldpad
lldptool
mergerepo
mergerepo.py
modifyrepo
modifyrepo.py
redefine_filedir
scl.bash
yum
yummain.py
yum-utils.bash

最佳答案

在谷歌搜索(以及 friend 的帮助)后,我找到了解决方案!

要修复文件名自动完成,请将以下内容添加到您的 ~/.bashrc 文件中:

complete -D -o default

引用:Bash completion for path in argument (with equals sign present)

关于linux - 参数后文件名的 Bash Tab 补全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33632668/

相关文章:

linux - dos2unix命令

Bash:如何从数字中提取数字

c# - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏

java - 使用完成建议程序进行ElasticSearch自动完成以返回已完成的文档

linux - BlueZ scotest 应用程序和 Linux 配置无法连接到 SCO 套接字

c - 在没有 LD_PRELOAD 的情况下在用户级别拦截系统调用

c++ - libnetfilter_queue 中没有 NF_REJECT 判定

bash - 防止期望在 EOF 上退出

linux - 加速使用多个查找命令的 bash 脚本

jquery-ui - 当第一个字符为 0 时,自动完成功能不会启动