Shell 命令完成

发布于 2024-11-01 21:26:34 字数 295 浏览 6 评论 0 原文

用户命令可以指定-complete=shellcmd选项。事实证明这非常令人失望,因为它的 :! 命令,它只是重复补全路径中的 shell 命令名称。

我想编写一个命令来完成命令名称,然后完成文件。有没有一种方便的方法来做到这一点,无论是使用内置的 vim 功能还是通过插件?

User commands can be given the -complete=shellcmd option. This turns out to be quite disappointing, since instead of working in the same way as vim's built-in completion for its :! command, it just repeatedly completes the names of shell commands in the path.

I'd like to write a command that completes command names and then files. Is there a convenient way to do this, either with built-in vim functionality or via an addon?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

篱下浅笙歌 2024-11-08 21:26:34

为什么不利用 bash 完成功能呢?您应该能够使用 /etc/bash_completion 源创建一个 bash shell,并通过管道与它交谈以询问它。这样你就拥有了所有的贝壳好东西。

当然,您可以像我一样,Cz 暂停 Vim 以返回到您的 shell。

Why not leverage bash-completion while you're at it? You should be able to fork a bash shell with /etc/bash_completion sourced and talk to it over pipes to interrogate it. That way you'd have all the shell goodies.

Of course, you could do as I do and C-z suspend Vim to drop back to your shell instead.

以往的大感动 2024-11-08 21:26:34

我最终编写了一个名为 shell_complete 的 vim 插件。它并不是真正的超级超级棒,但它确实做了我想要的东西,即或多或少模拟 :! 使用的完成。这就是说,它有点像“伪 shell 完成”:它完成第一个“单词”的命令,以及从那里输出的文件。基本的 \ 空格转义似乎工作正常。

伪 shell 完成

shell_complete 的安装有点复杂,特别是对于那些不熟悉 vim-addon-manager。该插件有几个依赖项,但它们都没有“发布”,也就是说 VAM 还不知道它们。您可以通过安装(通过 git clone ;有关更多信息,请参阅文档)另一个我称为 tt_addons。无论如何,完成此操作后,您应该可以先使用 :ActivateAddons tt_addons,然后使用 :ActivateAddons shell_complete

如果您不使用 VAM,则必须下载(更有可能的是 git clone)所有相关模块,然后将它们混合到您的 vim 目录中,或将它们制作为病原体包,或者你有什么。如果您确实想这样做,那么开始使用 VAM 可能是值得的。如果你是一个脾气暴躁的勒德分子,拒绝这样做,请告诉我,如果有适当的兴趣,我可能会把它放在 vim.org 上。

我认为如何使用 shell_complete 是非常明显的(至少从文档来看),但如果您想查看它的实际操作,您可以查看我的 reput 插件,它使用它来完成其 :RePutShell 命令。 reput 目前也只能通过 github 获得,并且同样的注意事项也适用于通过 VAM 安装。

使用 shell 进行实际 shell 完成

郑重声明,我认为 sehe 关于使用 shell 本身完成完成的建议完全是一闪而过。事实上,我花了相当长的时间来弄清楚如何做到这一点,并确定这是可能的,至少在理论上是这样。我最初认为这样做比执行 shell_complete 更容易,但事实证明 bash(如 vim)不提供任何编程方式来访问其完成设施,所以在使用 grep 和朋友抓取配置之后,您最终基本上必须在 bash 中重新实现它们。 Bash 在这种事情上很糟糕,所以我懦弱地拒绝参加这场战斗,至少目前是这样。

如果有人如此勇敢/愚蠢地接受这个标准,他们可能会利用我的痛苦编年史。我设法让它完成由自定义完成函数处理的完成。然而,这只是难题的一小部分,因为 bash 还提供了大约 6.5 种其他完成方法。它确实补充了 shell_complete 提供的功能,因此将两者协同合并为一种尴尬的、醉酒的 vim shell 完成 Voltron 可能是值得的。

I ended up writing a vim addon called shell_complete. It's not really super-megazord-awesome, but it does do pretty much what I was looking for, which is to more or less emulate the completion used by :!. This is to say that it's sort of "pseudo shell completion": it completes commands for the first "word", and files from there out. Basic \ escaping of spaces seems to work okay.

pseudo shell completion

Installation of shell_complete is sort of complicated, especially for those not familiar with vim-addon-manager. The addon has a couple of dependencies, and none of them are "published", which is to say that VAM doesn't know about them yet. You can let VAM know about them by installing (via git clone; see the docs for more info) another addon I've called tt_addons. Anyway, once you've done this, you should be able to just :ActivateAddons tt_addons and then :ActivateAddons shell_complete.

If you're not using VAM, you'll have to download (or git clone, more likely) all of the related modules, and then mix them into your vim directory, or make them pathogen bundles, or what have you. If you actually end up wanting to do this, it's likely worth your while to start using VAM. If you're some sort of curmudgeonly Luddite who refuses to do so, let me know and I might put the thing up on vim.org if there's decent interest.

I think it's pretty obvious (at least from the docs) how to use shell_complete, but if you would like to see it In Action you can check out my reput addon, which uses it to do completion on its :RePutShell command. reput is also currently only available through github, and the same caveats apply with respect to installation via VAM.

Actual shell completion using the shell

For the record, I think sehe's suggestion about using the shell itself to do completion is totally flash. I actually spent quite a while figuring out how to do this, and have determined that it's possible, at least in theory. I initially thought it would be easier to do it this way than by doing what shell_complete does, but it turns out that bash (like vim) doesn't provide any programmatic way to access its completion facilities, so you end up basically having to reimplement them in bash, after scraping the configuration using grep and friends. Bash sucks a lot for this sort of thing, so I'm cowardly refusing to fight that battle, at least for the moment.

Should someone be so brave/foolish as to take up this standard, they may avail themselves of the chronicles of my travails. I managed to get it to do completions that are handled by custom completion functions. However, this is only a small part of the puzzle, because bash also provides about 6½ other ways to do completion. It does sort of complement the functionality provided by shell_complete, so it might be worthwhile to synergistically merge the two into a sort of awkward, drunken Voltron of vim shell completion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文