7

How can I set sublime as default text editor for crontab?

I write cron jobs regularly so I would like to be able to edit crontab -e using sublime rather than Nano.

I have started with creating a .desktop file for Sublime.

I have also tried to use select-editor from Terminal but it does not display Sublime as an option.

Ubuntu 15.10

4 Answers 4

8

This is likely due to missing the -w/--wait flag which makes the command (subl) exit only after you close the file (the temporary cron file in this case). Without it, "cron" thinks you finished editing immediately as the command exits and any changes just ends up in the temporary file left for nothing.

So, one solution is:

Create a new file sublime-wait:

#!/bin/sh
subl -w $*

Add to your ~/.bashrc or ~/.profile:

export EDITOR='/path/to/sublime-wait'
1
  • thanks, it worked for me. Too bad there's no way to include small code snippets in comments, so I had to put them on another answer below.
    – dariox
    Jul 8, 2023 at 12:53
5

The sequence to pick the editor by cron is following, the first one wins:

  • VISUAL environment variable
  • EDITOR environment variable
  • /usr/bin/editor

So set the VISUAL as sublime's path e.g.:

export VISUAL=/usr/bin/subl

Add this to your ~/.bashrc for permanent assignment.


For just one run, send VISUAL to crontab's environment:

VISUAL=/usr/bin/subl crontab -e
1
  • 3
    It works in the sense that it opens the crontab -e file in Sublime BUT I am not able to save the file.in Sublime (Unable to save). Can you fix that too?
    – Vesa
    Nov 1, 2016 at 7:33
0

Setting the default editor in one of the above mentioned ways does work, e.g. editing the .bashrc file, exporting the variables EDITOR and VISUAL. But - at least in my environment - crontab -e does not wait for the editor to close the updated file and terminates saying "No modification made". Adding

export EDITOR="xed -w"
export VISUAL="xed -w"

solves the problem.

1
  • 1
    at least on macOS, spaces in the variable name are parsed as part of the file name and not an arg: $ crontab -e crontab: xed -w: No such file or directory
    – Carl Walsh
    Jul 31, 2020 at 21:16
0

Thanks to @thomas-jensen answer, I'd like to suggest a small modification, in order to avoid trouble when there is no GUI available, and applying what was suggested here:

#!/bin/sh
if [ x$DISPLAY != x ] ; then
  # GUI Enabled
  /opt/sublime_text/sublime_text -w $*
else
  vi $*
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .