如何从 shell 脚本启动编辑器?

发布于 2024-08-10 12:27:06 字数 305 浏览 5 评论 0原文

我希望我的 tcsh 脚本启动一个编辑器(例如,vi、emacs):

#!/bin/tcsh
vi my_file

这会使用 my_file 启动 vi,但首先显示警告“Vim:警告:输出不是到终端”,并且我的击键不会出现在屏幕。在我杀死 vi 后,我的终端窗口变得混乱(没有换行符),需要“重置”。我尝试了“emacs -nw”、“xemacs -nw”和 pico,结果相似。 “xemacs”可以工作,但会启动一个单独的窗口。我想重用相同的终端窗口。

有没有办法从脚本启动编辑器,以便它重用相同的终端窗口?

I would like my tcsh script to launch an editor (e.g., vi, emacs):

#!/bin/tcsh
vi my_file

This starts up vi with my_file but first displays a warning "Vim: Warning: Output is not to a terminal" and my keystrokes don't appear on the screen. After I kill vi, my terminal window is messed up (no newlines), requiring a "reset". I tried "emacs -nw", "xemacs -nw", and pico with similar results. "xemacs" works but launches a separate window. I want to reuse the same terminal window.

Is there a way to launch an editor from a script so that it reuses the same terminal window?

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

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

发布评论

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

评论(7

分分钟 2024-08-17 12:27:06

我回答了我自己的问题!您必须重定向终端输入和输出:

#!/bin/tcsh
vi my_file < `tty` > `tty`

I answered my own question! You have to redirect terminal input and output:

#!/bin/tcsh
vi my_file < `tty` > `tty`
倒带 2024-08-17 12:27:06

您收到错误的原因是,当您在环境中启动 shell 时,它会在 STDIN 和 STDOUT 未连接到 TTY 的子 shell 中启动 - 可能是因为这是在类似管道的情况下。重定向时,您将直接打开与设备的新连接。因此,例如,您的命令行变成

$ vi < `tty` > `tty`

所以

$ vi < /dev/ttys000 > /dev/ttys000

您并没有真正使用您的 STDIN/STDOUT,您正在创建两个新文件并将它们映射到 vi 进程的 STDIN/STDOUT。

现在,告诉我们您正在做什么,我们将告诉您如何避免这种混乱。

The reason you're getting the error is that when you start a shell in your environment, it's starting in a subshell that has STDIN and STDOUT not connected to a TTY — probably because this is in something like a pipeline. When you redirect, you're opening a new connection directly to the device. So, for example, your command line turns

$ vi < `tty` > `tty`

into

$ vi < /dev/ttys000 > /dev/ttys000

So you're not really using your old STDIN/STDOUT, you're creating two new files and mapping them to your vi process's STDIN/STDOUT.

Now, tell us what you're doing with this and we'll tell you how to avoid this kludge.

绝不服输 2024-08-17 12:27:06

我想做类似的事情。我想要一个别名来找到我正在处理的最后一个文件,并在 vi(1) 中打开它进行编辑。不管怎样,我不知道如何将其作为一个可读的别名(在 tcsh 中),所以我只是创建了一个丑陋的 shell 脚本(csh 因为我老了):

#!/bin/csh

set DIR = "~/www/TooMuchRock/shows/"

set file = $DIR`ls -t $DIR | head -1`
set tty = `tty`

vi $file <$tty >$tty

(1) kraftwerk:bin>哪个六
vi: 别名为 /usr/local/bin/vim -u ~/.exrc

I wanted to do something similar. I wanted an alias that would find the last file I was working on, and open it in vi(1) for editing. Anyway, I couldn't figure out how to do it as a readable alias (in tcsh) so I just created an ugly shell script (csh because I'm old) instead:

#!/bin/csh

set DIR = "~/www/TooMuchRock/shows/"

set file = $DIR`ls -t $DIR | head -1`
set tty = `tty`

vi $file <$tty >$tty

(1) kraftwerk:bin> which vi
vi: aliased to /usr/local/bin/vim -u ~/.exrc

伪装你 2024-08-17 12:27:06

绝对地。 :-)

编写脚本并让它调用 EDITOR 环境变量,您将其设置为“emacsclient”。然后启动 Emacs,执行 Mx server-start,切换到 shell 缓冲区 (Mx shell) 并执行脚本。 Emacsclient 将弹出要编辑的内容,Cx # 将充当“完成”命令,并根据您的选择将您带回脚本,编辑已完成或中止。

享受。

编辑:我想补充一点,现在 Emacs 是我的终端程序。我有几十个 shell 缓冲区,永远不必担心丢失输出,并且可以使用 Emacs 的所有功能来操作和分析终端输出。并让 Emacs 脚本生成 shell 的输入。实际上很棒。例如,在编辑源代码或处理邮件或执行大多数 Emacs 操作时,观看 Tomcat 输出在 shell 缓冲区中滚动是非常方便的。当 Tomcat 堆栈跟踪出现时,我可以快速响应它。

Absolutely. :-)

Write your script and have it call the EDITOR environment variable, which you will have set to "emacsclient". Then start up Emacs, execute M-x server-start, switch to a shell buffer (M-x shell) and execute your script. Emacsclient will pop up the thing to be edited and C-x # will act as a "done" command and take you back to your script with edits completed or aborted, as you choose.

Enjoy.

Edit: I meant to add that these days Emacs IS my terminal program. I have dozens of shell buffers and never have to worry about losing output and can use all the power of Emacs to manipulate and analyse the terminal output. And have Emacs scripts generate input to the shells. Awesome actually. For example, watching Tomcat output scroll by in a shell buffer while editing sources or processing mail or doing most any Emacs thing is very convenient. When a Tomcat stack trace appears I can quickly respond to it.

多孤肩上扛 2024-08-17 12:27:06

shell 脚本“while”循环中的“pinfo”也有同样的问题。该行可以在脚本中使用,它使用“ps”查找当前进程号“$$”的 tty,并将该 tty 存储在 $KEY_TTY 中:

  KEY_TTY=/dev/`ps | grep $ | tr -s '[:blank:]' | cut -d " " -f 3`

稍后在脚本中,只需调用仅限 tty 的 proggie,以 $KEY_TTY 作为输入,在我的例子中它是:

  pinfo -m $s $page < $KEY_TTY

对于 'vi' 它将是:

  vi $a < $KEY_TTY > $KEY_TTY

优点是脚本作为一个整体仍然可以接受 STDIN 输入,并且 'vi' (或其他)应该可以正常工作 - 没有必须记住在运行脚本之前设置任何环境变量。

Had the same trouble with 'pinfo' in a shell script 'while' loop. The line can be used in the script, it uses 'ps' to find the tty of the current process number, "$$", and stores that tty in $KEY_TTY:

  KEY_TTY=/dev/`ps | grep $ | tr -s '[:blank:]' | cut -d " " -f 3`

Later in the script, just call the tty-only proggie, with $KEY_TTY as input, in my case it was:

  pinfo -m $s $page < $KEY_TTY

For 'vi' it'd be:

  vi $a < $KEY_TTY > $KEY_TTY

The advantage is that the script as a whole can still accept STDIN input, and 'vi' (or whatever) should work fine -- without having to remember to set any environmental variables before running the script.

a√萤火虫的光℡ 2024-08-17 12:27:06

将终端 tty 设置为变量,然后通过该变量重定向编辑器 I/O。

在您的脚本中:

#!/bin/sh

ls | while read a; do vi $a < $MYTTY >$MYTTY; done

然后使用以下命令执行脚本:

$ MYTTY=`tty` ./myscript >/tmp/log

Set your terminal tty to a variable, and then redirect the editor i/o through that variable.

In your script:

#!/bin/sh

ls | while read a; do vi $a < $MYTTY >$MYTTY; done

And then execute the script with:

$ MYTTY=`tty` ./myscript >/tmp/log
赠佳期 2024-08-17 12:27:06

我能够在 bash+Cygwin+Terminator 下获得所需的行为:

#!/bin/bash
vim foo

运行脚本,vim 加载,没有错误消息,行为正常。然而,我们的设置之间无疑存在数十种差异,因此我无法冒险猜测是什么造成了差异。我很好奇它是什么,但你让它工作了,这是重要的部分。

I was able to get the desired behavior under bash+Cygwin+Terminator:

#!/bin/bash
vim foo

Run the script, vim loads, no error messages, behaves as normal. There are undoubtedly dozens of variations between our setups, however, so I can't hazard a guess as to what makes the difference. I'm curious what it is, but you got it working, which is the important part.

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