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


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
  • 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 '16 at 7:33

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:

subl -w $*

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

export EDITOR='/path/to/sublime-wait'

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
    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 '20 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.