3

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

3

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
  • 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
0

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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