90

"Joe's own editor" does not come naturally to me. How do I change to using nano or vim?

I've tried

export EDITOR=nano

but it doesn't seem to be respected. I'd like visudo to respect this as well.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
wmarbut
  • 1,001
  • 1
  • 8
  • 6
  • 1
    You're way should work but you have to manually export `EDITOR` each time you start the shell. Try this: `echo "export EDITOR=nano" >> ~/.bashrc`. –  Jul 10 '12 at 03:05
  • Thanks Bryan, but it actually wasn't working at all. I'm not sure what the deal was b/c this works in other distros and environments for me. –  Jul 10 '12 at 03:25
  • 1
    @BryanDunsmore No, not to `~/.bashrc`, to `~/.profile`. See [Alternative to .bashrc](http://unix.stackexchange.com/questions/3052/3085#3085) – Gilles 'SO- stop being evil' Jul 11 '12 at 00:15
  • It noticed this is the default also in Debian GNU/Linux buster (current stable). At least in the version provided by the OVH service provider. – Valerio Bozz Mar 10 '21 at 16:41

4 Answers4

133

To change the default editor at the system level:

sudo update-alternatives --config editor

and then follow the onscreen prompts.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Steve Robillard
  • 1,455
  • 1
  • 11
  • 7
22

The way to change the default editor for your account is to set the EDITOR environment variable. If that doesn't work for you, you've done something unusual. Check that you haven't also defined VISUAL, or if you have, give the two variables the same value (see VISUAL vs. EDITOR – what’s the difference?). Add these lines to your ~/.profile (note: not to ~/.bashrc):

EDITOR=nano
VISUAL=$EDITOR
export EDITOR VISUAL

Under the Debian policy, all programs are supposed to support EDITOR and VISUAL to set the default editor.

Under Debian and derivatives, you can use the alternatives mechanism to set the system-wide default editor, as mentioned by Steve Robillard: run update-alternatives --config editor as root.

Anthony Geoghegan
  • 12,605
  • 7
  • 59
  • 62
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • It is also changing an editor only for you, not for every user. There's a little possibility that someone may not be familiar with vim if you set it system-wide as default editor. – mykolaj Jan 04 '16 at 15:06
  • "Under the Debian policy, all programs are supposed to support EDITOR and VISUAL to set the default editor." In a twist of irony, `editor` is not one of those commands. :/ https://stackoverflow.com/a/73307112/3196753 – tresf Aug 10 '22 at 13:45
  • @tresf `editor` is the default to use when `EDITOR` and `VISUAL` are unset. There's a separate command which checks `EDITOR` and `VISUAL`, called `sensible-editor`. See the link for “Debian policy”. – Gilles 'SO- stop being evil' Aug 10 '22 at 13:49
19

The solution mentioned above works, but it isn't scriptable. If you want to do this in a scriptable (non-interactive) fashion, you should use --set:

# update-alternatives --set editor /usr/bin/vim.basic

You can get a list of the choices with:

$ update-alternatives --list editor
Joel Griffiths
  • 191
  • 1
  • 3
2

I came across the very same issue, however setting it via update-alternatives did not quite do the trick on a Raspbian Buster (10.2). Although I set vim.basic as my default editor (manually using update-alternatives --config editor), it had only a priority 30, while nano had a priority of 40.

root@rsyslog:~/scripts# update-alternatives --config editor
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    15        manual mode

Press <enter> to keep the current choice[*], or type selection number: 

I started poking around in the usual profile- and dot-files and came accross the following one:

root@rsyslog:~/scripts# cat /root/.selected_editor 
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/bin/nano"
root@rsyslog:~/scripts#

After setting vim.basic via /usr/bin/select-editor, the file contained vim.basic:

root@rsyslog:~/scripts# /usr/bin/select-editor

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

Choose 1-4 [1]: 2
root@rsyslog:~/scripts# cat /root/.selected_editor 
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/vim.basic"
root@rsyslog:~/scripts# 

Upon now I could do crontab -e with VIM again :).

Steffen
  • 161
  • 1
  • 2
  • 5