如何根据后缀从 bash 补全中删除文件名?

如何根据后缀从 bash 补全中删除文件名?

如果这是重复的,我深表歉意,但我(未成功)尝试编辑vim.

具体来说,在编辑 LaTeX 文档时,有很多我不想用vim.即我只想编辑.tex文件,而不是.aux以及 .pdf编译文档时生成的其他文件。

我玩过这个complete命令,但没有太多乐趣。

为什么以下命令不.aux从生成的文件名列表中删除文件?

$ complete -o default -X '^.*aux' vim

答案1

您想要的模式不是正则表达式,它是一个bashglob,您需要-f告诉它您正在完成文件。另外,您也不想default这样做,-o因为这会添加您刚刚过滤掉的文件名,要么删除该选项,要么选择不同的内容(我已经使用了它,dirnames这样它也可以完成目录)。以下应该完成这项工作:

complete -f -o dirnames -X '*.@(aux|pdf)' vim

man使用的 glob 的相关页面引用:

如果使用内置 shopt 启用 extglob shell 选项,则会识别多个扩展模式匹配运算符。在下面的描述中,模式列表是由 | 分隔的一个或多个模式的列表。复合图案可以使用以下子图案中的一种或多种来形成:

          ?(pattern-list)
                 Matches zero or one occurrence of the given patterns
          *(pattern-list)
                 Matches zero or more occurrences of the given patterns
          +(pattern-list)
                 Matches one or more occurrences of the given patterns
          @(pattern-list)
                 Matches one of the given patterns
          !(pattern-list)
                 Matches anything except one of the given patterns

extglob完成后应该已经启用bash,但如果没有,您也需要执行此操作。

相关内容