一般来说,编辑 crontab 时可以用 crontab -e
,它会读取 EDITOR
环境变量来决定需要打开的编辑器,比如:
EDITOR=emacsclient crontab -e
这时会用 emacsclient 来编辑 crontab 内容,只是需要注意一点,编辑完成后,需要用 C-x #(server-edit)
来退出。
如果使用 with-editor[1] 的话,可以进一步完善编辑 crontab 的体验。
首先定义下面的函数:
(defun my/edit-crontab ()
(interactive)
(let ((buf (get-buffer-create "*crontab*")))
(with-editor-async-shell-command "crontab -e"
buf buf)))
同时在 crontab 文件头添加以下内容,让其打开时,默认开启 with-editor-mode
。
# -*- mode: with-editor -*-
这样就能够和使用 magit 编辑 commit message 一样来编辑 crontab:
C-c C-c
确认C-c C-k
取消还有一点比较烦, async-shell-command
会默认打开一个 buffer,但是这个 buffer 又没什么用,可以用下面的方法将其隐藏掉:
(setq display-buffer-alist
'(("\\*crontab\\*" . (display-buffer-no-window . nil))))
最后,可以添加 crontab
到 recentf-exclude
变量中,这样编辑 crontab 的临时文件就不会进入 recentf 列表了。
[1]
with-editor: https://github.com/magit/with-editor
[2]
Emacs server - WikEmacs: https://wikemacs.org/wiki/Emacs_server
[3]
asynchronous - How to avoid pop-up of Async Shell Command buffer in Emacs? - Stack Overflow: https://stackoverflow.com/questions/13901955/how-to-avoid-pop-up-of-async-shell-command-buffer-in-emacs/47587185#47587185
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有