26

I'm trying to use sudoedit, but it keeps running the nano editor. My preferred editor is vim. How can I make it the default?

Looking at the man page, man sudoedit, I've noticed the following:

 EDITOR           Default editor to use in -e (sudoedit) mode if neither SUDO_EDITOR nor VISUAL is set.
 SUDO_EDITOR      Default editor to use in -e (sudoedit) mode.
 VISUAL           Default editor to use in -e (sudoedit) mode if SUDO_EDITOR is not set.

So I've set them all to /usr/bin/vim, but sudoedit /etc/hosts still uses nano. Am I missing something?

$ EDITOR=/usr/bin/vim
$ VISUAL=/usr/bin/vim
$ SUDO_EDITOR=/usr/bin/vim

$ echo $VISUAL
/usr/bin/vim

$ echo $EDITOR 
/usr/bin/vim

$ echo $SUDO_EDITOR 
/usr/bin/vim

$ sudoedit /etc/hosts # This is still using nano
  • What about sudo vim? – Cornelius Apr 24 '14 at 17:35
  • @Cornelius That works, that's my workaround for the problem at the moment. – Dan Apr 24 '14 at 17:36
  • 1
    try sudo visudo instead – animaletdesequia Apr 24 '14 at 17:37
  • @darent visudo: /etc/sudoers: Permission denied. What is that? – Cornelius Apr 24 '14 at 17:38
  • 2
    @darent visudo is a different program. It's just there to allow you to edit the sudoers file. man visudo – Dan Apr 24 '14 at 17:38
33

Run sudo update-alternatives --config editor and choose for vim. After this, sudoedit /etc/hosts should open /etc/hosts using vim.

Alternatively you can use sudo vim /etc/hosts.

| improve this answer | |
  • 1
    For future readers, keep in mind that sudo vim /etc/hosts is not exactly the same as sudoedit /etc/hosts even if both commands end up using vim. Using sudoedit is more secure compared to using sudo vim. – Dan Apr 1 at 7:17
  • The main purpose of sudoedit is to prevent running sudo vim ... - which grants you full root access to the machine. – Wernfried Domscheit May 7 at 18:19
15

Try exporting the variable i.e.:

$ SUDO_EDITOR=/usr/bin/vim
$ export SUDO_EDITOR

A new shell is started when you run the command and if this variable is not exported it will not exist in the new shell.

| improve this answer | |
  • 1
    Since the question is more related with exporting vs setting an environment variable and alternative way of doing sudo vim /etc/hosts is not relevant here, this should be the accepted answer. – BcK Aug 5 '19 at 6:58
  • @BcK The question is not about managing environment variables, it's about changing the default editor of the sudoedit command. Using environment variables to do that was something I tried because the manpages mentioned them. Using update-alternatives is a much better solution than setting an environment in my opinion as it sets the default editor globally, not just for sudoedit. Which is why I accepted the other answer. – Dan Apr 1 at 7:13
  • @Dan If you want to set an editor globally that should have been the question in the beginning. sudoedit manages this with SUDO_EDITOR variable as stated in the manpages and it only alters its own editor preference via this variable. Not everyone wants to use vim for everything, they just want to use different editors for different tasks. – BcK Apr 2 at 7:52

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.