38

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
7
  • What about sudo vim?
    – Cornelius
    Apr 24, 2014 at 17:35
  • @Cornelius That works, that's my workaround for the problem at the moment.
    – Dan
    Apr 24, 2014 at 17:36
  • @darent visudo: /etc/sudoers: Permission denied. What is that?
    – Cornelius
    Apr 24, 2014 at 17:38
  • 3
    @darent visudo is a different program. It's just there to allow you to edit the sudoers file. man visudo
    – Dan
    Apr 24, 2014 at 17:38
  • 1
    @MateenUlhaq I wasn't suggesting to use sudo vim, I am just asking how can use vim with sudoedit instead of nano.
    – Dan
    Aug 29, 2017 at 12:34

2 Answers 2

46

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.

6
  • 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 1, 2020 at 7:17
  • 7
    The main purpose of sudoedit is to prevent running sudo vim ... - which grants you full root access to the machine. May 7, 2020 at 18:19
  • does the "update-alternatives" method make it staying through reboot?
    – qdinar
    Jan 27, 2021 at 12:14
  • this command does not show all installed editors. as i see from an update-alternatives instruction ( 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, 2021 at 12:17
  • Between vim.tiny and vim.basic, which should I pick? The option for plain vim isn't listed.
    – Flimm
    Oct 17, 2023 at 9:34
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.

5
  • 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 5, 2019 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, 2020 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, 2020 at 7:52
  • does this method make it staying through reboot?
    – qdinar
    Jan 27, 2021 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, 2022 at 7:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .