Den*_*uzé 12 macos makefile autocomplete
Makefile的目标在Linux上完成,但是AFAICS,而不是Mac OS(10.8.5).
是否可以使用此操作系统完成工作?
Rus*_*ore 22
这似乎为我在El Capitan上实现了简单的bash完成:
# .bashrc
function _makefile_targets {
local curr_arg;
local targets;
# Find makefile targets available in the current directory
targets=''
if [[ -e "$(pwd)/Makefile" ]]; then
targets=$( \
grep -oE '^[a-zA-Z0-9_-]+:' Makefile \
| sed 's/://' \
| tr '\n' ' ' \
)
fi
# Filter targets based on user input to the bash completion
curr_arg=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "${targets[@]}" -- $curr_arg ) );
}
complete -F _makefile_targets make
Run Code Online (Sandbox Code Playgroud)
这是如何工作的:
complete -F [function name] [command name] - 这个bash builtin为[命令名]注册了一个由bash函数[function name]生成的新完成.所以在上面的代码中,如果你输入make [TAB][TAB]shell,你将触发该_makefile_targets()函数.
if [[-e"$(pwd)/ Makefile"]]; 然后 - 确保当前目录中有一个Makefile,否则不要尝试bash完成.
grep -oE'^ [a-zA-Z0-9 _-] +:'Makefile - 使用正则表达式过滤Makefile的每一行,目标名称如"test:".-o表示仅返回匹配行的部分.例如,给定Makefile目标,如"test:build package",将只返回"test:"
| sed's /://' - 获取grep结果,从行尾删除冒号
| tr'\n''' - 将所有目标放在一条线上,用一个空格隔开
在bash完成函数中,complete为您设置几个环境变量.COMP_WORDS是基于用户输入内容的可用bash完成选择列表的数组.COMP_CWORD是当前所选单词的索引.
另一个非常神奇的内置compgen函数将单独列出空格列表并使用当前选定的单词对其进行过滤.我一点也不清楚它是如何工作的.
因此,底线是函数中的最后两行过滤我们的makefile目标列表(存储在其中$targets)并将它们推送到一个数组中COMPREPLY.bash完成读取并显示COMPREPLY 为shell中的选项.
灵感来自:
如果您使用 bash 和 homebrew:
brew install bash-completion
Run Code Online (Sandbox Code Playgroud)
遵循他们的文档:
将以下行添加到您的 ~/.bash_profile 中:
brew install bash-completion
Run Code Online (Sandbox Code Playgroud)
重新启动您的终端或
source ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)
小智 6
对于 macOS Monterey,可以执行以下操作:
编辑.zprofile主目录中的文件。这可以使用文本编辑器来完成。小路:~/.zprofile
将以下行添加到文件末尾:
zstyle ':completion:*:*:make:*' tag-order 'targets'
autoload -Uz compinit && compinit
Run Code Online (Sandbox Code Playgroud)
之后,一切正常,当您按 TAB 时提示会起作用。
与上面指出的方法不同,此方法在进入终端时不会显示任何额外内容。
这对我在标准 zsh 终端的 Catalina 上有用
编辑.zshrc在您的主目录中命名的文件,这可以在终端中使用此命令完成nano ~/.zshrc
添加以下几行
zstyle ':completion:*:*:make:*' tag-order 'targets'
autoload -U compinit && compinit
Run Code Online (Sandbox Code Playgroud)
ctrl + x然后保存退出并保存Y| 归档时间: |
|
| 查看次数: |
2391 次 |
| 最近记录: |