从 vi 编辑器中,使用任意程序打开任意类型的文件

发布于 2024-11-27 10:17:16 字数 1054 浏览 0 评论 0原文

vi 编辑器有一个非常有用的命令 gf,它允许在新的 vi 窗口中打开路径位于下面的文件vi 编辑器中的光标。我正在尝试推广此功能,以便可以通过 shell 命令(例如 open)过滤文件名,从 vi 编辑器中打开任何类型的文件。

但是,根据反复试验和仔细阅读 *nix open 命令的 man 页,我相信不可能打开名称/路径已传输的文件通过管道/重定向。例如,以下非常简单的命令会失败,因为 open 无法接受管道输入作为参数。

回显文件名.txt |打开

除了 open 之外,也许还有其他函数可以在这种情况下使用?

以下是我尝试使用 vi 命令打开一个文件的选择,该文件的路径是使用 vi 视觉模式在 vi 中选择的:

:'<,'>; !打开

:'<,'>; !回声|打开

:'<,'>; !猫 |打开

:'<,'> 行话仅指通过 vi 视觉模式突出显示的文本。 ! 表示以下文本应使用默认 shell 作为 shell 命令执行。因此,这些行尝试通过各种 shell 命令过滤突出显示的文本,但无济于事。

上面三个尝试通过 open 命令过滤文件名的例子都失败了。如果有人对如何完成我所描述的任务有任何建议,请分享。一般情况(打开通过管道/重定向接收路径的文件)或特殊情况(使用 vi 命令行打开路径包含在文本文件中的文件)的解决方案将不胜感激。

作为奖励,我希望能够通过简单地将光标定位在文件名上来从 vi 中打开文件(与 gf 命令的情况一样) ,而不是使用 vi 视觉模式突出显示路径。

The vi editor has a very useful command gf, which allows one to open — in a new vi window — the file whose path is situated beneath the cursor in the vi editor. I am attempting to generalize this feature such that a file of any type can be opened from within the vi editor by filtering the file name through a shell command such as open.

However, based on trial and error and perusal of the man page for the *nix open command, I believe it is not possible to open a file whose name/path is transmitted via pipe/redirection. As an example, the following very simple command fails because open cannot accept piped input as an argument.

echo file_name.txt | open

Perhaps there are other functions besides open that can be used in this situation?

Here is a selection of vi commands I have tried to use to open a file whose path was selected within vi using the vi visual mode:

:'<,'> ! open

:'<,'> ! echo | open

:'<,'> ! cat | open

Note: the :'<,'> jargon simply refers to the text that has been highlighted via vi visual mode. The ! signifies that the following text should be executed as a shell command using the default shell. Thus, these lines attempt to filter highlighted text through various shell commands, to no avail.

The three above examples attempting to filter the file name through the open command fail to work. If anyone has any suggestions on how to accomplish the task I have described, please share. Solutions to either the general case (opening a file whose path is received via a pipe/redirect) or the particular case (opening a file whose path is contained in a text file by using the vi command line) would be greatly appreciated.

As a bonus, I would like to be able to open the file from within vi by simply positioning the cursor over the file name (as is the case for the gf command), rather than by highlighting the path using the vi visual mode.

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

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

发布评论

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

评论(1

且行且努力 2024-12-04 10:17:16

您需要 xargs(1)

You want xargs(1).

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