【问题标题】:bash completion of makefile targetbash完成makefile目标
【发布时间】:2011-05-10 10:54:20
【问题描述】:

假设我有一个简单的 makefile,例如:

hello:
   echo "hello world"

bye:
   echo "bye bye"

然后在 bash 中我想要类似的东西:

制作h

所以它可以完成到

打个招呼

我找到了一种简单的方法,例如创建空文件 hellobye,但我正在寻找更复杂的东西。

【问题讨论】:

    标签: makefile bash-completion


    【解决方案1】:

    将此添加到您的 ~/.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>

    注意事项和注意事项:

    1. 您的 make 文件名为“GNUMakefile”或除“Makefile”或“makefile”之外的任何其他名称。如果您经常遇到此类标题,请考虑相应更改正则表达式?akefile

    2. 进行更改后忘记获取您的 ~/.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
    • 我认为处理cwd中没有Makefile的情况会很好:complete -W "`([[ -r Makefile ]] && grep -oE '^[a-zA-Z0- 9_-]+:([^=]|$)' Makefile || cat /dev/null) | sed 's/[^a-zA-Z0-9_-]*$//'`" make
    • 您的解决方案未检测到包含点“.”的命令
    • @ahmedbhs 已更新以包括对“.”的识别
    • 他们不应该考虑 .PHONY 和 .env etcc 所以这是我的建议完整 -W "`grep -oE '^[a-zA-Z0-9_-][a-zA-Z0- 9_.-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_-]*$//'`" make
    【解决方案2】:

    这就是你要找的吗?

    http://freshmeat.net/projects/bashcompletion/

    make [Tab] 将完成所有 Makefile 中的目标。这个项目是 设想生产可编程的 完成例程最 常见的 Linux/UNIX 命令,减少 键入系统管理员的数量和 程序员每天需要做的 基础。

    【讨论】:

      【解决方案3】:

      有一个名为bash-completion 的有用软件包可用于大多数操作系统。它包括 Makefile 完成。

      (如果您使用的是 macOS 和 Homebrew,您可以通过 brew install bash-completion 获取此信息。)

      【讨论】:

      • 对于 macOS - bash-completion@2 与 bash-completion 直接冲突。如果您的 bash >= 4.1,鼓励使用 @2 版本。它似乎没有包含 Makefile 完成,因此在其他答案中提出的 bashrc 中添加自定义规则可能会有所帮助。
      【解决方案4】:

      这似乎至少在 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
      

      【讨论】:

        【解决方案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
        

        【讨论】:

          【解决方案6】:

          在 Ubuntu 10.04 中,获取以下文件:

          . /etc/bash_completion
          

          或在

          中取消注释
          /etc/bash.bashrc
          

          【讨论】:

            猜你喜欢
            • 2013-04-20
            • 1970-01-01
            • 2021-09-27
            • 1970-01-01
            • 2010-10-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多