为什么“定位文件名|” xargs vim”导致奇怪的终端行为?

发布于 2024-12-17 14:01:45 字数 306 浏览 4 评论 0原文

当我执行“locate 50local.policy | xargs vim”时,出现错误“Vim: Warnung: Die Eingabe kommt nicht von einem Terminal”(翻译:Vim:警告:输入不是来自终端)。

我可以使用 vim 成功编辑,但关闭它后,我的终端表现得很奇怪(我无法输入字母,当我按 Enter 键时,shell 提示符会重复出现。 当我使用“xargs gedit”执行此操作时,它不会产生这些问题。

我将 Ubuntu 11.10 与 Gnome 3 和 Gnome-Terminal 3.0.1 一起使用。

When I do "locate 50local.policy | xargs vim", I get the error "Vim: Warnung: Die Eingabe kommt nicht von einem Terminal" (translation: Vim: Warning: The input does not come from a terminal).

I can edit successfully with vim but after I close it my terminal behaves strangely (I can't type letters and when I hit enter the shell prompt simply gets repeated.
When I do it with "xargs gedit" it does not create those problems.

I use Ubuntu 11.10 with Gnome 3 and Gnome-Terminal 3.0.1.

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

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

发布评论

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

评论(3

孤寂小茶 2024-12-24 14:01:45

Vim 期望连接到真实终端并发送适合该终端的代码。

使用最简单的解决方法重置终端

reset

locate 50local.policy | xargs gvim

基本原理 gui vim 不需要终端

否则:

vim $(locate 50local.policy)

基本原理 vim 直接连接到终端启动(而不是作为 xargs 下的子进程,后者又在子 shell 中运行stdin/stdout 连接到管道而不是终端)。 这就像说

vim /usr/some/dir/50local.policy /usr/local/some/dir/50local.policy

或者,

您可以通过不使用参数启动 vim,而是从 vim 添加参数来避免这个问题!事实上,Vim 在运行 shell 方面比 shell 在运行 vim 方面要好得多。

在 vim 中:

:args `locate 50local.policy`
:rewind

这会将参数列表设置为从刻度之间的 shell 命令返回的文件; :rewind 然后转到该列表中的第一个文件。
如果您正在编辑多个匹配项,请尝试以下操作:

:w|next

此命令序列(用 | 分隔)将当前缓冲区写入文件,然后转到 args 列表中的下一个文件。

Vim expects to be connected to a real terminal and sends codes appropriate to that.

Reset the terminal with

reset

The easiest workaround:

locate 50local.policy | xargs gvim

Rationale gui vim doesn't require a terminal

Otherwise:

vim $(locate 50local.policy)

Rationale vim is started directly connected to the terminal (instead of as a child process under xargs which in turn runs in a subshell with stdin/stdout connected to pipes instead of a terminal). It is like saying

vim /usr/some/dir/50local.policy /usr/local/some/dir/50local.policy

Alternatively

You can dodge the issue by not starting vim with the arguments, but adding the arguments from vim! Vim is in fact a lot better at running shells than shells are at running vim.

Whilst in vim:

:args `locate 50local.policy`
:rewind

This sets the argument list to the files returned from the shell command between the ticks; :rewind then goes to the first file from that list.
If you were editing multiple matches, try this:

:w|next

This sequence of commands (separated by |) writes the current buffer to file, then goes to the next file in the args list.

独自唱情﹋歌 2024-12-24 14:01:45

另一种替代方法是使用 -o 选项执行 xargs。从手册页:

-o      Reopen stdin as /dev/tty in the child process before executing
        the command.  This is useful if you want xargs to run an interac-
        tive application.

注意,-o 是 xargs 的 BSD 扩展。

达到相同效果的更便携的方法是:

xargs sh -c 'vim "$@" < /dev/tty' vim

An other alternative is to execute xargs with the -o option. From the man page:

-o      Reopen stdin as /dev/tty in the child process before executing
        the command.  This is useful if you want xargs to run an interac-
        tive application.

Note, -o is a BSD extension to xargs.

A more portable means to achieve the same effect is:

xargs sh -c 'vim "$@" < /dev/tty' vim
怎会甘心 2024-12-24 14:01:45

虽然“重置”可以解决问题,但您还可以使用以下命令显式地重新激活回显行为:

stty echo

While 'reset' fixes the problem, you can also explicitely re-activate the echo behaviour with:

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