bash 完成 makefile 目标
假设我有一个简单的 makefile,例如:
hello:
echo "hello world"
bye:
echo "bye bye"
然后在 bash 中我想要类似的东西:
使 h <标签>
所以它可以完成
打个招呼
我找到了一种简单的方法,例如创建空文件 hello
和 bye
,但我正在寻找更复杂的方法。
Suppose I have a simple makefile like:
hello:
echo "hello world"
bye:
echo "bye bye"
Then in bash I want something like:
make h < tab >
so it can complete to
make hello
I found a simple way like creating empty files hello
and bye
but I'm looking for something more sophisticated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将其添加到您的 ~/.bash_profile 文件或 ~/.bashrc 文件中
这将在您的 Makefile 中搜索标题为“Makefile”或“makefile”的目标(注意大写 < code>? 通配符 (
?akefile
) 使用 grep,并将其通过管道传输到 bash 中的complete
命令,该命令用于指定如何自动完成参数。-W
标志表示complete
命令的输入将是一个单词列表,这是通过将 grep 的结果传递给sed
来完成的,sed
会排列将其转换为所需的单词列表格式。注意事项和陷阱:
您的 make 文件名为“GNUMakefile”或“Makefile”或“makefile”以外的任何其他名称。如果您经常遇到此类标题,请考虑相应地更改正则表达式
?akefile
。进行更改后忘记获取 ~/.bash_profile 或 ~/.bashrc 文件。我添加这个看似微不足道的细节,因为对于外行来说它是陌生的。
要使 bash 文件的任何更改生效,请使用以下命令获取它们
或
PSsource 它们。现在,您还可以通过按两次 [Tab] 来显示可能的 make 目标,就像在 bash 补全中一样。只需确保在键入 [Tab] 两次之前在命令 make 之后添加一个空格即可。
Add this in your ~/.bash_profile file or ~/.bashrc file
This searches for a target in your Makefile titled 'Makefile' or 'makefile' (note the capital
?
wildcard in?akefile
) using grep, and pipes it over to thecomplete
command in bash which is used to specify how arguments are autocompleted. The-W
flag denotes that the input to thecomplete
command will be a wordlist which is accomplished by passing the results of grep throughsed
which arranges it into the desirable wordlist format.Caveats and gotchas:
Your make file is named 'GNUMakefile' or anything else other than 'Makefile' or 'makefile'. If you frequently encounter such titles consider changing the regular expression
?akefile
accordingly.Forgetting to source your ~/.bash_profile or ~/.bashrc file after making the changes. I add this seemingly trivial detail since, to the uninitiated it is unfamiliar.
For any change to your bash files to take effect, source them using the command
or
PS. You also now have the added ability to display the possible make targets by pressing [Tab] twice just like in bash completion. Just make sure you add a space after the command make before typing [Tab] twice.
有一个名为 bash-completion 的有用软件包适用于大多数操作系统。它包括 Makefile 完成。
(如果您使用的是 macOS 和 Homebrew,您可以通过
brew install bash-completion
获取此信息.)There's a useful package called bash-completion available for most every OS. It includes Makefile completion.
(If you're using macOS and Homebrew, you can get this via
brew install bash-completion
.)这可能是您正在寻找的东西吗?
http://freshmeat.net/projects/bashcompletion/
Could this be what you're looking for?
http://freshmeat.net/projects/bashcompletion/
这似乎是至少在 Debian Lenny 中的默认设置:
该文件的标头指出:
This seems to be default in at least Debian Lenny:
The header of this file states:
这是一个查看 .PHONY: 声明的完成脚本。
Here is a completion script that looks at the .PHONY: declaration.
类固醇完成 Makefile!
我在正常完成时遇到了 2 个问题:
问题 #1
有时你有想要调用的目标,例如
makegreet:hi
和makegreet:hola
有点像命名空间Makefile
目标名称。因此,您的Makefile
最终看起来像:在这种情况下,
:
之后的自动完成不会显示,因为它在Makefile如上所示。问题 #2
没有办法使用箭头键(或 CTRL-p / CTRL-)浏览与我的输入匹配的所有
Makefile
目标的列表。 n) 在我的bash
shell 中。基本上,我想在目标上使用类似模糊搜索的方法(即
fzf
)。FZF Repo: https://github.com/junegunn/fzf
解决方案
安装 FZF 依赖项
使用 Homebrew
您可以使用 Homebrew(在 macOS 或 Linux 上)
安装fzf。
使用 Linux 软件包管理器
sudo apk add fzf
sudo apt-get install fzf
conda install -c conda-forge fzf
sudo dnf install fzf
nix-env -iA nixpkgs.fzf
sudo pacman -S fzf
pkg install fzf
pkgin install fzf
pkg_add fzf
sudo xbps-install -S fzf
sudo zypper install fzf
FZF 和
:
兼容的自动完成命令将其放入您的
.bashrc
现在只需输入
make
然后按一下键就可以了!演示:在行动!
然后你可以使用如下:
make using
fzf
Makefile completion on steroids!
I had 2 problems with the normal completions:
Problem #1
Sometimes you have targets you want to call like
make greet:hi
andmake greet:hola
sort of like namespacingMakefile
target names. So yourMakefile
ends up looking like:In this case the auto-completions after
:
don't show up as it uses\:
in the Makefile as shown above.Problem #2
There wasn't a way to navigate through the list of all
Makefile
targets that match my input using arrow keys (orCTRL-p
/CTRL-n
) in mybash
shell.Basically, I wanted to use fuzzy search like approach on the targets (i.e.
fzf
).FZF Repo: https://github.com/junegunn/fzf
Solution
Install FZF Dependency
Using Homebrew
You can use Homebrew (on macOS or Linux)
to install fzf.
Using Linux package managers
sudo apk add fzf
sudo apt-get install fzf
conda install -c conda-forge fzf
sudo dnf install fzf
nix-env -iA nixpkgs.fzf
sudo pacman -S fzf
pkg install fzf
pkgin install fzf
pkg_add fzf
sudo xbps-install -S fzf
sudo zypper install fzf
FZF and
:
compatible auto-complete commandPut this in your
.bashrc
Now just typing
make
and then hitting the key will work!DEMO: in action!
Then you can use as following:
make using
fzf
我添加了所以我遵循 Makefile 中的“include”指令。所以我的 .bashrc 看起来像这样:
I added so I follow "include" directives in Makefile. So my .bashrc looks like this:
在 Ubuntu 10.04 中,获取以下文件:
或取消注释
In Ubuntu 10.04, source the following file:
or uncomment it in