Change the default editor when sudo visudo

18,222

Solution 1

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.)
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

Solution 2

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.

Solution 3

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.

Share:
18,222

Related videos on Youtube

Cbhihe
Author by

Cbhihe

I like strawberries, therefore I'm not a bot. # sed -i -e 's/Micro[$s]oft/Linux/g' World VMS -> *nix -> Apple -> Windows -> Ubuntu -> Archlinux (and not coming back)

Updated on September 18, 2022

Comments

  • Cbhihe
    Cbhihe over 1 year

    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 ?

    • fkraiem
      fkraiem over 9 years
      Hve you checked man visudo?
    • Volker Siegel
      Volker Siegel over 9 years
      I have found out some more things since I wrote it - I hope to add more later; The Linux distrbutions handle it differently.
    • Cbhihe
      Cbhihe over 9 years
      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.
    • Volker Siegel
      Volker Siegel over 9 years
      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
    • Volker Siegel
      Volker Siegel over 9 years
      Also, did you try the update-alternatives I also added?
    • Trevor Boyd Smith
      Trevor Boyd Smith about 8 years
      askubuntu.com/questions/539243/… has more upvotes for the question/accepted-answer... (as of this date-time: 31 upvotes vs 16 for this question).
    • Cbhihe
      Cbhihe about 8 years
      @TrevorBoydSmith: I don't understand yr comment. The other post is indeed a dupe. It came significantly after this one and its author should have consulted AU and other sources before posting. So should have all those who provided answers.
  • Eliah Kagan
    Eliah Kagan over 9 years
    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).
  • Volker Siegel
    Volker Siegel over 9 years
    It works with sudo -E visudo - which should certainly be in the answer, right.
  • Cbhihe
    Cbhihe over 9 years
    +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.
  • Volker Siegel
    Volker Siegel over 9 years
    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.
  • Cbhihe
    Cbhihe over 9 years
    @VolkerSiegel: I imagine that you refer to the env_reset default in /etc/sudoers. It is set just so in my case.
  • Арсен Мирзаян
    Арсен Мирзаян over 8 years
    doesn't this create a security hole as per the visudo man page? Note that this can be a security hole since it allows the user to execute any program they wish simply by setting VISUAL or EDITOR.
  • Cbhihe
    Cbhihe about 7 years
    +1: Good one, and I had not noticed it when I first started this thread, thanks.
  • Cbhihe
    Cbhihe about 6 years
    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 !
  • botkop
    botkop over 5 years
    definitely the best answer
  • mementum
    mementum about 5 years
    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.
  • Antonios Hadjigeorgalis
    Antonios Hadjigeorgalis about 4 years
    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
  • lainatnavi
    lainatnavi about 4 years
    Oh, this answer already exists :D
  • John
    John over 2 years
    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