194

I am trying to change the default editor from nano to vim.

I have run the following commands:

sudo update-alternatives --config editor

and

update-alternatives --config editor

Both now output:

  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

I have restarted my ssh session and restarted sshd but crontab -e still opens in nano

What else do I need to do?

7ochem
  • 191
austinbv
  • 1,923
  • 1
    I don't think any of these answers actually address the question do they? – geotheory Apr 13 '16 at 23:31
  • Here is a more extensive answer: https://askubuntu.com/questions/977538/16-04-crontab-e-editor-settings-python-syntax-highlighting-in-vim – SDsolar Nov 17 '17 at 21:23

10 Answers10

300

Just simply run select-editor, this will let you choose any editor you want.

Mostafa Shahverdy
  • 5,082
  • 2
  • 21
  • 34
150

Try your ssh session followed by

export EDITOR=vim

or possibly

export EDITOR=/usr/bin/vim.basic

The format of setting of the EDITOR variable depends on which shell you are using.

In Ubuntu you can set the EDITOR environment variable on logon by adding the above to the ~/.profile

Your SSH session will read a similar file (if it exists) on the remote host called ~/.ssh/environment. This has to be specifically defined by the ssh daemon config. See man sshd_config and look for PermitUserEnvironment for more details.

fossfreedom
  • 172,746
36

If you only want to choose the editor temporarily, you can do the following

EDITOR=nano crontab -e

If you want do this for root user then:

sudo EDITOR=nano crontab -e

This sets the EDITOR environment variable for the command

Gelldur
  • 263
  • 3
  • 8
Luke Madhanga
  • 463
  • 4
  • 6
  • Thank you. This helped me to edit the sudo/root's crontab without needing to change the root user's editor or something (on the shared VM that others might want a different editor for). – Max Starkenburg Jul 23 '21 at 18:25
  • This helped me a lot too, but how to change the crontab editor permanent? I changed it as a text editor for sudoedit, but it still uses vim for crontab (unless I use this answer. However I would prefer not having to type sudo EDITOR=nano every time). And... yes, I know, many of you disagree, but I do prefer nano, sorry. – Andyc Apr 07 '23 at 19:16
28

My personal preference...

cd /bin
mv nano nano_must_die
ln -s /usr/bin/vim nano
Oli
  • 293,335
Dave
  • 337
  • 3
  • 2
  • I love it, worked out well enough for me after update-alternatives didnt work. – Cole Busby Jan 28 '14 at 19:13
  • 12
    I wouldn't know what's wrong with nano for admins who don't see value in learning vi commands just to tweak some config files. – Max Apr 27 '17 at 09:04
  • 7
    I guess do whatever you like on your own personal machine. But on a machine shared with anyone else, this suggestion essentially disables nano for all users in a way that when a user specifically requests nano, they get vim instead. What's the point of that? If they wanted vim, they would ask for it. If the issue here is that the system is configured to automatically invoke nano in some circumstance where you personally prefer vim, then change the configuration. – gwideman Sep 05 '18 at 00:30
  • 1
    I guess this was an ironic/satiric joke for vi fans, but not everybody gets a joke, I guess. – Mladen B. Feb 18 '21 at 13:37
  • why are you killing nano? – alper Mar 28 '21 at 21:25
  • 1
    Downvote. I get that this is a joke but jokes aren't useful answers. Also, I never want to work at a place where someone would actually rename nano rather than configuring things properly. – Clonkex Jun 21 '23 at 04:34
17

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.

Perhaps you have the EDITOR enivronment variable set to nano?

aureianimus
  • 3,536
14

On my Ubuntu 12.04 computer, crontab uses the ~/.selected_editor file, which contains the path of the selected editor. Edit it:

nano ~/.selected_editor

I have edited it directly or used select-editor, which is a script to do the same thing. Edit the following line:

SELECTED_EDITOR="/usr/bin/vim.basic" 

IHMO people should not be changing anything in /etc or /bin to do this. It is a user level thing, not a system wide task.

muru
  • 197,895
  • 55
  • 485
  • 740
NormanD
  • 141
4

I've had the same problem - crontab -e relies on select-editor, visudo relies on the config of "alternatives"

My solution:

run both commands as root

# update-alternatives --install /usr/bin/editor editor /usr/bin/sensible-editor 50

# select-editor 

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/sensible-editor
  3. /usr/bin/vim.basic
  4. /usr/bin/vim.tiny

Choose 1-4 [1]: 3
julian
  • 41
3

After trying the answers above, the only thing that worked for me (in Debian strech) was to remove ~/.selected_editor by running:

rm ~/.selected_editor

And then select the new editor next time you run crontab -e.

Eliah Kagan
  • 117,780
Marcos
  • 131
3

In addition to checking that the environmental variable EDITOR is set correctly, you should also check to make sure the variable VISUAL is also set correctly.

VISUAL will override EDITOR

From the documentation:

The -e option is used to edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.

For example, if you set

$ export EDITOR=vim
$ export VISUAL=nano

Then the command crontab -e will still open in nano.

You should set both to be the editor of choice, ala vim for me:

$ export EDITOR=vim
$ export VISUAL=vim

Then the command crontab -e will open in vim.

Nate Houk
  • 131
  • Are there corresponding commands for exporting a variable for root (sudo) and possibly make the changes permanent (stay there after reboot)? – Andyc Apr 07 '23 at 19:21
3

I was having difficulties with select-editor and update-alternatives, my solution was to simply edit the link:

  1. sudo rm /etc/alternatives/editor
  2. sudo ln -s /usr/bin/vim /etc/alternatives/editor

editor now opens Vim