39

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
Dan
  • 13,119

2 Answers2

47

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.

Louis Matthijssen
  • 11,885
  • 6
  • 44
  • 50
  • 8
    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 01 '20 at 07:17
  • 7
    The main purpose of sudoedit is to prevent running sudo vim ... - which grants you full root access to the machine. – Wernfried Domscheit May 07 '20 at 18:19
  • does the "update-alternatives" method make it staying through reboot? – qdinar Jan 27 '21 at 12:14
  • this command does not show all installed editors. as i see from an update-alternatives instruction ( https://linuxhint.com/update_alternatives_ubuntu/ ) it is possible to add apps that are not yet listed. i would like you also show how to do that. – qdinar Jan 27 '21 at 12:17
  • Between vim.tiny and vim.basic, which should I pick? The option for plain vim isn't listed. – Flimm Oct 17 '23 at 09:34
  • Just to clarify for future readers what it is about sudo vim that is insecure, sudoedit only gives read and write privileges for the one specific file. sudo vim, on the other hand, gives full sudo privilege to anything the user does while in vim. Which includes, through the use of :!, running any terminal command, and also anything any malicious plugin might like to do (when was the last time you checked all your plugins to make sure they didn't silently check for sudo privileges and then did something scary if they had them?) – Arthur Jan 16 '24 at 10:59
21

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.

Elin Y.
  • 893
  • 3
    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 05 '19 at 06: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 01 '20 at 07: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 02 '20 at 07:52
  • does this method make it staying through reboot? – qdinar Jan 27 '21 at 12:02
  • @Dan for me, update-alternatives did nothing (probably because there was no default editors config), but this answer solved my issue. – Dan M. Apr 21 '22 at 07:09