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

muru
  • 207,228
Vesa
  • 336

5 Answers5

9

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'
6

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
grooveplex
  • 2,506
  • 3
  • 27
  • 35
heemayl
  • 93,925
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.

Achim
  • 1
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
dariox
  • 146
0

I'm using ZSH and wanted to open crontab via subl as well. Here is how for that setup:

In your ~/.zshrc export:

export VISUAL="subl -w"
export EDITOR="$VISUAL"

(Thanks to all answers for the -w wait flag)

Open a new shell or source again via source ~/.zshrc

Then run crontab -e

In your crontab file, make sure to add SHELL=/usr/bin/zsh at the top if you want to use ZSH in your cron script.

Add your crontab at the end of the file. Make sure you have a new line at the very and of the file. Save in sublime and close the tab/window. Check your shell again:

crontab -e

no crontab for <user> - using an empty one

crontab: installing new crontab new crontab file is missing newline before EOF, can't install. Do you want to retry the same edit? (y/n) Y

crontab: installing new crontab