19

I know I can set the EDITOR variable and edit my crontab with emacs but that requires starting the process from a shell with

$ crontab -e

My question is how can I directly modify my crontab if I'm inside of emacs already? I tried running starting up eshell and running crontab -e from there but the terminal complains:

$ crontab -e
emacs: Terminal type "dumb" is not powerful enough to run Emacs.
It lacks the ability to position the cursor.
If that is not the actual type of terminal you have,
use the Bourne shell command `TERM=... export TERM' (C-shell:
`setenv TERM ...') to specify the correct type.  It may be necessary
to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.
/usr/bin/crontab: no changes made to crontab

If using crontab in eshell worked that would be a step in the right direction but it's still not exactly what I was hoping for. Attempts to execute crontab -e from within an M-x term buffer in emacs fail as well (at least on OpenBSD 5.6 with emacs 24.3.1). In the ideal case, there would be a crontab mode that allowed me to

M-x crontab

that fires up a major mode and allowed me to do the usual stuff I do at the command line with crontab and have it all bound to keys. I figured for sure there'd be a mode or something for this but I haven't had much luck locating one.

unclejamil
  • 303
  • 2
  • 7
  • There's a `crontab-mode` in MELPA, did you try it? (I didn't, so I don't know whether it works or whether it does what you want). – wvxvw Mar 17 '15 at 16:01
  • What about using `M-x term` for launching a not so dumb terminal? – Nsukami _ Mar 17 '15 at 16:02
  • @wvxvw: Yeah I did an M-x list-packages first and found that mode but the description indicates that this is just a highlighting mode that is meant to help the guy who made it work with his personal stuff he keeps under rcs. – unclejamil Mar 17 '15 at 16:06
  • @Nsukami_: That can work but it's not really what I'm after in the end. It keeps me in emacs so it's definitely a step in the right direction and better than leaving emacs but seems like a good mode for crontab is in order (something like M-x crontab) that pulls my crontab and has keys bound to different things I usually do with switches at the command line with crontab. – unclejamil Mar 17 '15 at 16:08
  • @Nsukami_: I've updated the question to clarify what I'm looking for based on our discussion here in the comments. BTW, thank you for the suggestion. – unclejamil Mar 17 '15 at 16:13

3 Answers3

29

You could try to use the with-editor package:

(defun crontab-e ()
    "Run `crontab -e' in a emacs buffer."
    (interactive)
    (with-editor-async-shell-command "crontab -e"))

it will run crontab in a subshell, using the current Emacs as editor.

ppareit
  • 141
  • 4
Rémi
  • 1,607
  • 11
  • 13
  • Ha! Pretty sweet. Just pulled down the with-editor package and tried it out. Works like a charm! Nice call. I'll wait a day to see if someone has cranked out a full on mode for crontab and if no one comes back with anything I'll mark this as the answer. – unclejamil Mar 17 '15 at 16:31
  • This works well if there are no errors. Can it be made to check for errors in the Async Command buffer and allow the user to correct the errors? – Daniel Doherty Jan 06 '23 at 10:56
1

First setting the environment variable in a shell/eshell inside emacs:

export VISUAL="emacsclient"

Then, make sure the emacs server is running by M-x server-start. Now, crontab -e within emacs shell/eshell directly opens the cron file inside emacs as a separate buffer.

N.B. make sure replace emacsclient with the absolute path if the folder containing it is not already included in the environment variables.

Boson Bear
  • 111
  • 3
0

You can also edit a crontab without using a shell by exporting the current content to a file

crontab -l > crontabFile

Editing that file in emacs and running

crontab crontabFile

To save that file in crontab

KhalfaniW
  • 337
  • 4
  • 8