【发布时间】:2011-05-10 10:54:20
【问题描述】:
假设我有一个简单的 makefile,例如:
hello:
echo "hello world"
bye:
echo "bye bye"
然后在 bash 中我想要类似的东西:
制作h
所以它可以完成到
打个招呼
我找到了一种简单的方法,例如创建空文件 hello 和 bye,但我正在寻找更复杂的东西。
【问题讨论】:
假设我有一个简单的 makefile,例如:
hello:
echo "hello world"
bye:
echo "bye bye"
然后在 bash 中我想要类似的东西:
制作h
所以它可以完成到
打个招呼
我找到了一种简单的方法,例如创建空文件 hello 和 bye,但我正在寻找更复杂的东西。
【问题讨论】:
将此添加到您的 ~/.bash_profile 文件或 ~/.bashrc 文件中
complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' ?akefile | sed 's/[^a-zA-Z0-9_.-]*$//'\`" make
这会使用 grep 在您的 Makefile 中搜索名为“Makefile”或“makefile”的目标(注意 ?akefile 中的大写 ? 通配符),并将其通过管道传递给 bash 中使用的 complete 命令指定如何自动完成参数。 -W 标志表示complete 命令的输入将是一个单词列表,它通过将 grep 的结果传递给 sed 来完成,sed 将其排列成所需的 wordlist 格式。 p>
注意事项和注意事项:
您的 make 文件名为“GNUMakefile”或除“Makefile”或“makefile”之外的任何其他名称。如果您经常遇到此类标题,请考虑相应更改正则表达式?akefile。
进行更改后忘记获取您的 ~/.bash_profile 或 ~/.bashrc 文件。我添加了这个看似微不足道的细节,因为对于外行来说这是不熟悉的。 要使对 bash 文件的任何更改生效,请使用命令source它们
source ~/.bashrc
或
source ~/.bash_profile
PS。您现在还可以通过按两次 [Tab] 来显示 可能的 生成目标,就像在 bash 完成中一样。只需确保在两次键入 [Tab] 之前在命令 make 之后添加一个空格。
【讨论】:
complete -W "`make -qp | sed -n -E 's/^([^.#\s][^:=]*)(:$|:\s+.*$)/\1/p' | sort -u'`" make
这就是你要找的吗?
http://freshmeat.net/projects/bashcompletion/
make [Tab] 将完成所有 Makefile 中的目标。这个项目是 设想生产可编程的 完成例程最 常见的 Linux/UNIX 命令,减少 键入系统管理员的数量和 程序员每天需要做的 基础。
【讨论】:
有一个名为bash-completion 的有用软件包可用于大多数操作系统。它包括 Makefile 完成。
(如果您使用的是 macOS 和 Homebrew,您可以通过 brew install bash-completion 获取此信息。)
【讨论】:
这似乎至少在 Debian Lenny 中是默认的:
$ grep Makefile /etc/bash_completion
# make reads `GNUmakefile', then `makefile', then `Makefile'
elif [ -f ${makef_dir}/Makefile ]; then
makef=${makef_dir}/Makefile
# before we scan for targets, see if a Makefile name was
# deal with included Makefiles
此文件的标题说明:
# The latest version of this software can be obtained here:
#
# http://bash-completion.alioth.debian.org/
#
# RELEASE: 20080617.5
【讨论】:
这是一个查看 .PHONY: 声明的完成脚本。
_make_phony_words() {
local opt_revert
if [ -n "${BASH_VERSION:-}" ]; then
shopt -q nullglob || {
opt_revert=1 ; shopt -s nullglob ;
}
elif [ -n "${ZSH_VERSION:-}" ]; then
[[ -o nullglob ]] || {
opt_revert=1 ; setopt nullglob
}
fi
for f in ./?akefile ./*.make ; do
sed -nEe '/^.PHONY/ { s/^.PHONY:[ ]?// ; p ; } ' "$f" | tr ' ' $'\n' | sort -u
done
if [ -n "$opt_revert" ]; then
[ -n "${ZSH_VERSION:-}" ] && unsetopt nullglob
[ -n "${BASH_VERSION:-}" ] && shopt -u nullglob
fi
unset opt_revert
}
_make_phony_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY+=( $(compgen -W "$( _make_phony_words )" -- ${cur}) )
}
complete -F _make_phony_complete make
【讨论】:
在 Ubuntu 10.04 中,获取以下文件:
. /etc/bash_completion
或在
中取消注释/etc/bash.bashrc
【讨论】: