返回

随时随地掌握 Emacs shell 命令进度:让其发送桌面通知

见解分享

让 Emacs shell 命令发送桌面通知

作为一名 Emacs 铁杆用户,我发现自己在 shell 中运行命令时经常会遇到一个问题:忘记跟踪命令的运行状态。这可能是因为我切换了缓冲区,或者只是因为我的注意力被其他事情分散了。

不过,有一个解决办法:让 Emacs shell 命令发送桌面通知。这将使我随时了解命令的进度,而无需不断切换缓冲区。

配置 Emacs 发送桌面通知

要配置 Emacs 发送桌面通知,你需要安装 desktop-notifications 软件包。对于 Ubuntu 用户,可以使用以下命令安装:

sudo apt install libnotify-dev

安装后,你需要在 Emacs 配置文件中添加以下代码:

(require 'desktop-notifications)
(desktop-notifications-default-provider 'libnotify)
(add-hook 'eshell-hook '(lambda () (desktop-notifications-enable-provider 'eshell)))

这将启用桌面通知,并将其配置为在 Emacs shell 中使用。

创建发送通知的函数

接下来,我们需要创建一个函数来发送桌面通知。为此,可以将以下代码添加到你的 Emacs 配置文件中:

(defun eshell-send-notification (message)
  "Send a desktop notification with the given message."
  (desktop-notifications-notify
   'eshell
   :summary "Emacs Shell"
   :body message))

这个函数接受一个消息字符串作为参数,并将其发送为桌面通知。

在 shell 命令中使用通知函数

现在,我们可以使用 eshell-send-notification 函数在 shell 命令中发送通知。为此,只需在命令的末尾添加以下代码:

&& eshell-send-notification "Command completed"

这将导致在命令完成后发送一条桌面通知,内容为“Command completed”。

示例用法

以下是一个使用该函数发送通知的示例:

eshell-send-notification "Hello, world!"

这将在你的桌面上显示一条桌面通知,内容为“Hello, world!”。

自定义通知

你可以通过修改 desktop-notifications-notify 函数的参数来自定义通知的外观和行为。例如,要更改摘要,可以使用以下代码:

(desktop-notifications-notify
 'eshell
 :summary "Custom Summary"
 :body message)

这将更改通知的摘要为“Custom Summary”。

高级用法

对于高级用法,你可以使用 desktop-notifications-config 函数来配置通知的各种方面,包括图标、超时和声音。有关更多详细信息,请参阅 Emacs 文档。

结论

通过让 Emacs shell 命令发送桌面通知,你可以随时了解命令的进度,而无需不断切换缓冲区。这提高了你的工作效率,并有助于你专注于当前的任务。