简体   繁体   English

在bash脚本中使用emacs打开和关闭txt文件

[英]Open and close txt files with emacs in bash script

I wrote a script which opens several text files that I need during my work (eg journals etc.). 我编写了一个脚本,该脚本打开了我在工作期间需要的几个文本文件(例如日记本等)。 The script looks like: 该脚本如下所示:

#! /bin/bash

DOCPATH=$HOME/Documents
NUMDOCPATH=$DOCPATH/Numerics

emacs $DOCPATH/UTA_WorkProtocol.txt &

emacs $NUMDOCPATH/NumProtocol.txt &

emacs $NUMDOCPATH/CITCOM/Protocol.txt &

That works fine and the files are opend properly. 效果很好,文件已正确打开。 However, at the end I would like to create another script to save and close those files again. 但是,最后,我想创建另一个脚本来再次保存并关闭这些文件。 But, I haven't been able to find any commands to save and/or close open emacs files running in the background using a bash script. 但是,我无法使用bash脚本找到任何命令来保存和/或关闭在后台运行的打开的emacs文件。 I would be more convenient to just run such a script instead of doing CX Cs in each txt file as I might be adding new files to the "opening"script as well. 我会更方便的是只运行这样的脚本,而不是在每个txt文件中执行CX C,因为我可能也会向“ opening”脚本中添加新文件。

Could any more experienced person help me with that, please. 请更多有经验的人帮助我。 Thanks! 谢谢!

This is a bit of a hack, but bash scripts aren't really suited to this type of UI scripting. 这有点骇人听闻,但bash脚本并不真正适合此类UI脚本。

#! /bin/bash

DOCPATH=$HOME/Documents
NUMDOCPATH=$DOCPATH/Numerics

emacs $DOCPATH/UTA_WorkProtocol.txt &
emacs $NUMDOCPATH/NumProtocol.txt &
emacs $NUMDOCPATH/CITCOM/Protocol.txt &

emacs   # *Not* in the background
kill $(jobs -p)

When you are ready to close all the backgrounded instances of emacs, just close the one that is in the foreground. 当您准备关闭所有emacs的后台实例时,只需关闭前台的实例即可。 This will cause the final emacs to exit, which will allow the script to continue to the kill command, which will kill the remaining background jobs. 这将导致最终的emacs退出,这将使脚本继续执行kill命令,这将杀死其余的后台作业。

Note that the final emacs could be any command that blocks the script from completing and can be easily killed itself. 请注意,最终的emacs可以是任何阻止脚本完成并且可以轻易杀死的命令。

Must you use bash ? 您必须使用bash吗? It's easy in Emacs lisp : 在Emacs lisp中很容易:

(defun open-files ()
  (interactive)
  (find-file "UTA_WorkProtocol.txt")
  (find-file "NumProtocol.txt")
  (find-file "CITCOM/Protocol.txt"))

(defun close-files ()
  (interactive)
  (save-buffers-kill-emacs)

Then open a single instance of Emacs and run those commands in Mx . 然后打开一个Emacs实例并在Mx运行这些命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM