Change the default editor when sudo visudo
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.
Related videos on Youtube
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, 2022Comments
-
Cbhihe over 1 year
When doing
sudo visudo
, the default editor isnano
.
I want to change it tovi
orvim
.I already made
vim
the default editor, and use it as a substitute forgedit
to open php, .txt, .c and .h files, by placing avim.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 tonano
.Any clue ?
-
fkraiem over 9 yearsHve you checked
man visudo
? -
Volker Siegel over 9 yearsI have found out some more things since I wrote it - I hope to add more later; The Linux distrbutions handle it differently.
-
Cbhihe over 9 yearsYes 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 over 9 yearsI added the
sudo -E visudo
later in the answer, did you try that? The -E makessudo
not remove all env vars. It does because ubuntu has a lineDefaults env_reset
in/etc/sudoers
-
Volker Siegel over 9 yearsAlso, did you try the
update-alternatives
I also added? -
Trevor Boyd Smith about 8 yearsaskubuntu.com/questions/539243/… has more upvotes for the question/accepted-answer... (as of this date-time: 31 upvotes vs 16 for this question).
-
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 over 9 yearsDid 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
andEDITOR
are unset by default, not set tonano
.VISUAL
andEDITOR
aren't the mechanism through whichvisudo
selectsnano
in the default configuration (except insofar as it consults them, finds they're unset or blank, and moves on). -
Volker Siegel over 9 yearsIt works with
sudo -E visudo
- which should certainly be in the answer, right. -
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 over 9 yearsOh, 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 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 yearsdoesn'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 about 7 years+1: Good one, and I had not noticed it when I first started this thread, thanks.
-
Cbhihe about 6 yearsThis 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
andvim
actively coexist on any one user's profile. Howevernano
andvim
do happen together in the wild... Keepnano
! -
botkop over 5 yearsdefinitely the best answer
-
mementum about 5 years
emacs
andvim
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 about 4 yearsMaybe 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 about 4 yearsOh, this answer already exists :D
-
John over 2 yearsOn 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