Change default text editor for crontab to vim

104

20

I'm using ubuntu 9.10 and the default text editor is nano, which i hate. (doesn't everyone?)

Normally it's not a problem as i just vi or gedit everything but crontab -e is opening with nano. I tried changing it to vim using sudo update-alternatives --config editor and selecting option 3 ("/usr/bin/vim.basic"). This has changed it for sudo and non-sudo alike. But crontab -e still opens nano. Any ideas? max

Max Williams

Posted 2011-05-10T16:13:21.293

Reputation: 2 237

Answers

130

The crontab -e command will check the environment variables $EDITOR and $VISUAL for an override of the default text editor, so...

export VISUAL=vim

or

export EDITOR=vim

should do the trick.

njd

Posted 2011-05-10T16:13:21.293

Reputation: 9 743

7Remember if you are editing another users's crontab, use sudo -E crontab -e where sudo -E specifies use your env vars. – MarkHu – 2016-01-28T19:15:12.967

90

In ubuntu, try run: select-editor, which interactively creates ~/.selected_editor:

# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/vim.basic"

smintz

Posted 2011-05-10T16:13:21.293

Reputation: 1 086

2Yup, this changes ~/.sensible_editor used by /usr/bin/sensible-editor. It seems that in the absence of the environment variables specifying the editor, crontab runs sensible-editor not editor as the former allows per-user configuration. – eel ghEEz – 2015-02-25T05:25:24.697

2@MaxWilliams, running select-editor will not show the previously made selection, which is stored in ~/.sensible_editor. – eel ghEEz – 2015-02-25T05:27:11.627

5@eelghEEz - Do you mean ~/.selected_editor? That's what's on my system and what I've seen elsewhere. – Wilson F – 2016-06-13T18:03:48.107

works for debian too – JSBach – 2017-09-03T17:46:17.463

i tried this (under sudo and my user) and whatever option i changed, when i called it again it was still on nano. Fixed it anyway with EDITOR env var but thought i'd better mention it anyway. Cheers – Max Williams – 2011-05-12T09:34:39.230

4Above answers didn't work...this does. – mlissner – 2013-01-25T19:59:29.057

12

If you hate nano so much you can just uninstall it:

sudo apt-get remove nano

crontab should then just default to the next EDITOR (for me it was vim.basic).

complistic

Posted 2011-05-10T16:13:21.293

Reputation: 313

1This is the only solution that worked for me. Tried 4-5 things before this :) – Charlie Wynn – 2015-11-24T01:54:39.833

This is by far the best solution if, as the OP implies, you never need nano. Not sure why it didn't occur to me before - probably that I didn't realise crontab would just default to the next available editor! Awesome – Luke – 2017-03-20T16:08:41.347

We run our cron jobs with super-user account, but login to a dev account. So from dev account I need to do sudo crontab -e. I set export EDITOR=vim in both super-user's and the dev account's .bashrc, but sudo crontab -e was still opening up in nano. After uninstalling nano, it opens vim. Thanks! – arun – 2013-07-27T17:06:40.473

11

From man crontab:

The -e option is used to edit the  current  crontab  using  the  editor
specified  by  the  VISUAL  or EDITOR environment variables.  After you
exit from the editor, the modified crontab will be installed  automati‐
cally.  If  neither  of  the environment variables is defined, then the
default editor /usr/bin/editor is used.

Add to your ~/.bashrc:

export EDITOR=vim

cYrus

Posted 2011-05-10T16:13:21.293

Reputation: 18 102

4

The better choice is to set alternative of editor (not just one user) :

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/vim 100

Gilles Quenot

Posted 2011-05-10T16:13:21.293

Reputation: 3 111

3Why is it better to change the setting for other users, who did not asked for the change? – ceving – 2017-04-25T15:37:37.493

3

Unfortunately I can not comment or vote.

On Ubuntu the configuration file is called ~/.selected_editor

With the following command you can select the default editor again:

$ select-editor

Removing the file in your home directory also works.

$ rm ~/.selected_editor

Only setting the variables $VISUAL or $EDITOR will work but is only persistent if you write it to a script which is executed in your environment.

Add to your rc file

$ echo "export VISUAL=/usr/bin/vi" >> ~/.bashrc

But i wouldn't recommend to use the last solution.

André

Posted 2011-05-10T16:13:21.293

Reputation: 47

If you don't recommend it why did you provide it? – Ramhound – 2016-01-05T11:52:38.440

Just for completion and because other people may prefer that way. – André – 2016-01-06T14:46:34.760

3

export EDITOR=vi && crontab -e 

works on debian squeeze

Torsten Zenk

Posted 2011-05-10T16:13:21.293

Reputation: 31

2A little explanation would go a long way. – ChrisF – 2012-10-01T21:15:32.397

3this sets the EDITOR environment variable and subsequently edits the crontab file, EDITOR=vim crontab -e will work as well, but only one time. – 0x4a6f4672 – 2012-11-21T16:56:00.133

0

You should best remove the ~/.sensible_editor file and then running crontab -e will prompt you to choose the preferred editor.
From then on your preference will be remembered in the ~/.sensible_editor file.

Jiju Thomas Mathew

Posted 2011-05-10T16:13:21.293

Reputation: 111

not sure why this was down voted it is exactly what I need and worked perfectly. – rob – 2015-10-28T13:28:13.810

Ubuntu does not seem to know emacsclient. – ceving – 2017-04-25T15:35:07.540

-1

for Debian, use :

sudo update-alternatives --config editor command

and

 ----------------------------------------------------------
06  * 0          /bin/nano            40       
07  1            /bin/nano            40       
08  2            /usr/bin/vim.basic   30        
09  3            /usr/bin/vim.tiny    10        

select '2' and press enter. Got it!

reasonpun

Posted 2011-05-10T16:13:21.293

Reputation: 1

Welcome to Super User! Please read the question again carefully. Your answer does not answer the original question. The OP already tried this (it's in the question) and it did not fix his problem. – DavidPostill – 2015-12-19T18:03:15.373

-1

Easiest would be to get rid of the product you don't want in its entirety. All other config changes would be automatical.

apt-get install vim -y && apt-get remove nano -y

Osiris

Posted 2011-05-10T16:13:21.293

Reputation: 1

-1

On older machines like some Debian ones, this works also and is the most portable solution.

mv /usr/bin/editor /usr/bin/.editor
ln -s $(which vim) /usr/bin/editor

hlupaco

Posted 2011-05-10T16:13:21.293

Reputation: 1

No, don't mess with anything in /usr/bin manually; these locations are managed by dpkg and should not be manipulated directly. – tripleee – 2018-04-12T11:51:50.057