4

I am trying to figure out why I don't have syntax highlighting when editing my crontab.

I have both $EDITOR and $VISUAL set to /usr/bin/vim:

> echo $EDITOR
/usr/bin/vim
> echo $VISUAL
/usr/bin/vim

If I save the crontab to a file and edit it with vim syntax highlighting is enabled.

> crontab -l > saved_cronab
> /usr/bin/vim saved_crontab

And if I use :syntax on while editing the crotab nothing changes

How can I enable highlighting when editing crontab with crontab -e?

ben
  • 151
  • 1
  • 4

4 Answers4

7

vim doesn't know, that your file saved_crontab is a crontab. Therefore, you don't get a special syntax highlighting for crontabs. Setting the filetype to crontab worked for me. I used:

:set ft=crontab

You can see the value of filetype with:

:set ft?
Ronin Tom
  • 334
  • 1
  • 4
  • You can also permanently set the modeline for this file by adding a modeline like `# vim: ft=crontab` to the top of the file. However modelines are disabled by default for security reasons as root and by some distributions. – Nova Apr 05 '13 at 13:39
  • Agreed: setting `EDITOR` or `VISUAL` is irrelevant, since OP ran vim directly. Further clarification would improve the answer, but the information in this question addresses the problem. – Thomas Dickey Mar 27 '16 at 18:32
  • @ThomasDickey OP said using vim directly on a file works, but the question isn't about that - they're asking about `crontab -e`. When `EDITOR` isn't set, crontab seems to actually be using `vi`, which while it does run vim, disables syntax highlighting. – Izkata May 30 '19 at 16:36
5

The accepted answer is quite poorly explained IMHO, so here's something more to help people solve this issue.

I still fail to understand why crontab refuses to pick the editor specified with

sudo update-alternatives --config editor

in my case:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
* 3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

And in fact everything correctly points to vim.basic:

#> ll /usr/bin/editor
lrwxrwxrwx 1 root root 24 Oct 20  2014 /usr/bin/editor -> /etc/alternatives/editor

#> ll /etc/alternatives/editor
lrwxrwxrwx 1 root root 18 Jun 20  2016 /etc/alternatives/editor -> /usr/bin/vim.basic

#> ll /usr/bin/vim.basic
-rwxr-xr-x 1 root root 2.4M Nov 24  2016 /usr/bin/vim.basic

#> ll /usr/bin/vim
lrwxrwxrwx 1 root root 21 Oct 20  2014 /usr/bin/vim -> /etc/alternatives/vim

But crontab still doesn't care. So, as correctly pointed by dsznajder, the solution is to explicitly tell crontab what the $EDITOR is, via environment variable.

One can export it via .bashrc or .profile, but given that crontab is the only one to ignore /etc/alternatives/, I preferred to create an alias just for him to make him feel the shame of requiring a custom alias to work (just like for dmesg -T).

alias crontab='EDITOR=vim crontab'

That's it, fixed ✔ :)

Avio
  • 870
  • 2
  • 10
  • 10
5

Did you export these variables (export EDITOR VISUAL)?

dsznajder
  • 182
  • 2
  • 3
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://serverfault.com/faq#reputation) you will be able to [comment on any post](http://serverfault.com/privileges/comment). – mdpc Mar 22 '13 at 00:40
  • 3
    @mdpc Actually it does answer the question, and I'll bet it's the right answer, too. – Michael Hampton Mar 22 '13 at 01:30
  • @mdpc Probably `export` is an answer, but you are right - I bit misunderstand "you can answer but not comment" policy. Sorry about that. Should I remove my another "clarification request" (http://serverfault.com/questions/490255/apache-doesnt-parse-php-on-vhost-while-it-works-on-default/490290#490290) ? – dsznajder Mar 22 '13 at 05:47
4

This worked for me:

EDITOR="/usr/bin/vim"
export EDITOR

Add this to ~/.bash_profile to enable this for current user.
Add this to /etc/profile/any_file_you_like to enable this for all users.