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 ?

  • Hve you checked man visudo?
    – fkraiem
    Oct 1 '14 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. Oct 2 '14 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
    Oct 2 '14 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 Oct 2 '14 at 19:14
  • Also, did you try the update-alternatives I also added? Oct 2 '14 at 19:16

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"

or more POSIX-correct

VISUAL="vim" ; export VISUAL

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.)
In Ubuntu, the system default seems to be set with sudo update-alternatives --config editor. It shows a menu to change the current association.

See section ENVIRONMENT in man visudo:

  VISUAL           Invoked by visudo as the editor to use

  EDITOR           Used by visudo if VISUAL is not set
  • 2
    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). Oct 1 '14 at 22:33
  • 4
    It works with sudo -E visudo - which should certainly be in the answer, right. Oct 1 '14 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
    Oct 2 '14 at 20:58
  • 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. Oct 2 '14 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
    Oct 3 '14 at 6:33

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
    +1: Good one, and I had not noticed it when I first started this thread, thanks.
    – Cbhihe
    Jan 19 '17 at 15:37

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.

  • 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
    Apr 7 '18 at 13:30
  • 3
    definitely the best answer
    – botkop
    Aug 21 '18 at 5:41
  • 1
    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
    Feb 5 '19 at 9:06
  • 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 Feb 2 '20 at 22:23
  • 1
    Oh, this answer already exists :D
    – lainatnavi
    Apr 17 '20 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.