38

When doing sudo visudo, the default editor is nano.
I want to change it to vi or vim.

I already made vim the default editor, and use it as a substitute for gedit to open php, .txt, .c and .h files, by placing a vim.desktop file in ~/.local/share/applications/ and by correctly editing either ~/.local/share/applications/mimeapps.list or /etc/gnome/defaults.list. However apparently this does not apply to nano.

Any clue ?

7
  • Hve you checked man visudo?
    – fkraiem
    Commented Oct 1, 2014 at 20:53
  • I have found out some more things since I wrote it - I hope to add more later; The Linux distrbutions handle it differently. Commented Oct 2, 2014 at 17:44
  • Yes and I set my env-var VISUAL to vim and "exported" in .bashrc and in .bash_profile and then sourced the shell but to no avail. Even logged out and relogged-in in session to make sure.
    – Cbhihe
    Commented Oct 2, 2014 at 17:48
  • 2
    I added the sudo -E visudo later in the answer, did you try that? The -E makes sudo not remove all env vars. It does because ubuntu has a line Defaults env_reset in /etc/sudoers Commented Oct 2, 2014 at 19:14
  • Also, did you try the update-alternatives I also added? Commented Oct 2, 2014 at 19:16

4 Answers 4

27

The problem is not that it does not apply to nano, it's that it does not apply to the shell:

Just set the VISUAL environment variable:

export VISUAL=vim

Add this too ~/.bashrc to make it permanent.

As you seem to use vim in general, set both VISUAL and EDITOR:

export VISUAL="vim"
export EDITOR="$VISUAL"

or more POSIX-correct

VISUAL="vim" ; export VISUAL
EDITOR="$VISUAL" ; export EDITOR

I assume nano was the value of one or both variables.

To make use of the editor in visudo actually, we need to handle that sudo does not keep the environment variables by normally. The option -E changes that.

sudo -E visudo

Without the -E here, you would end up with a default of nano again


The two variables where in use long before files named *.desktop or mime* even existed.
(And the impressive thing is: they were actually used as a common standard.)

See section ENVIRONMENT in man visudo:

  VISUAL           Invoked by visudo as the editor to use

  EDITOR           Used by visudo if VISUAL is not set

Permanent fix using update-alternatives

In Ubuntu, the system default seems to be set with sudo update-alternatives --config editor. It shows a menu to change the current association to a vi of your choice.

9
  • 4
    Did you try it? By default sudo unsets most environment variables from the environment for the command it runs. VISUAL=vim sudo visudo (and the variations you suggest) doesn't work for me and shouldn't be expected to work. sudo VISUAL=vim visudo does work, though. (I've just tried both again, to be sure.) By the way, VISUAL and EDITOR are unset by default, not set to nano. VISUAL and EDITOR aren't the mechanism through which visudo selects nano in the default configuration (except insofar as it consults them, finds they're unset or blank, and moves on). Commented Oct 1, 2014 at 22:33
  • 6
    It works with sudo -E visudo - which should certainly be in the answer, right. Commented Oct 1, 2014 at 22:46
  • +1 and I do accept yr excellent answer, Volker. "Besten Dank !" I still got an error message with the -E flag, about not being allowed to pass on environment variables, but then I su'ed from a restricted access user to one with admin-privilege and bingo ! All works well.
    – Cbhihe
    Commented Oct 2, 2014 at 20:58
  • 1
    Oh, I'm happy to add one more thing that can go wrong/can help ;) Could you check the options (lines with Default ) in your /etc/sudoers? I think there is an option that forbids that, but is was not in my default file. Commented Oct 2, 2014 at 21:01
  • @VolkerSiegel: I imagine that you refer to the env_reset default in /etc/sudoers. It is set just so in my case.
    – Cbhihe
    Commented Oct 3, 2014 at 6:33
14

If you never plan to use nano, you can also simply remove it. Then the system will use vi/vim as the default.

sudo apt-get purge nano

I know it is not the official answer, but it is one of the first commands for me after installing Ubuntu.

5
  • This definitely qualifies as "necroposting", but hey ... Yr answer is interesting, inasmuch it deals with Ubuntu. Some other distros (ArchLinux among them) only have the editor(s) you installed in the 1st place, so you have neither the pbm I encountered with Ubuntu, nor the need to resort to the measure you suggest. In the case at hand (ubuntu + nano + vim), I feel that many may have a need for options in terms of editors. It's true one rarely see the likes of emacs and vim actively coexist on any one user's profile. However nano and vim do happen together in the wild... Keep nano !
    – Cbhihe
    Commented Apr 7, 2018 at 13:30
  • 7
    definitely the best answer
    – botkop
    Commented Aug 21, 2018 at 5:41
  • 3
    emacs and vim do happily coexist and are constantly used. nano is just like the pest, the sooner it is removed from the system the better. I do understand that some people may find it comfortable, but this answer is perfectly valid.
    – mementum
    Commented Feb 5, 2019 at 9:06
  • 1
    Maybe this works because when you remove nano, this happens update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode Commented Feb 2, 2020 at 22:23
  • 1
    Oh, this answer already exists :D
    – lainatnavi
    Commented Apr 17, 2020 at 19:34
9

As described in this answer, add

Defaults editor=/path/to/editor

to the sudoers file.

Note: this will only work if the file being edited contains the Defaults editor=/path/to/editor line or includes a file that contains it.

For example: visudo -f /etc/sudoers.d/my_sudoers_extension will default to Nano.

2
  • 2
    +1: Good one, and I had not noticed it when I first started this thread, thanks.
    – Cbhihe
    Commented Jan 19, 2017 at 15:37
  • On macOS you can just comment out a line in the sudoers file instead of adding a new "Defaults" line. #Defaults env_keep += "EDITOR VISUAL" This allows you to choose when launching visudo which editor: sudo EDITOR=/path/to/editor visudo
    – John
    Commented Dec 14, 2021 at 23:14
2

The problem with using:

export VISUAL="vim"
export EDITOR="$VISUAL"

Is that sudo visudo will sanitize the users environment variables so those variables are set in the root environment. If you are the only user of the machine (or everyone prefers VIM) you could always set those into root's environment too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .