9

I found another way to set Vim as the default text editor in Ubuntu but I would like to know why this tutorial is not working for me:

https://www.a2hosting.com/kb/developer-corner/linux/setting-the-default-text-editor-in-linux

Here's what I did:

I edited ~/.bashrc by adding these 2 lines:

export EDITOR='vi'
export VISUAL='vi'

Then I typed the command:

source .bashrc

I also tried to log out of my account and then log back in but it's still not working.

When I try to open the /etc/sudoers file it opens with Nano text editor instead of Vim.

3
  • 2
    When I try to open the /etc/sudoers file -- What command did you use to open that? Commented Feb 6, 2023 at 17:28
  • @Archisman Panigrahi sudo visudo /etc/sudoers Commented Feb 6, 2023 at 21:04
  • when you edited .bashrc that was for your userid. when you ran the sudo command visudo was running as userid root. You can make the changes in root's .bashrc, or you can change it system-wide as noted by others. Commented Nov 10, 2023 at 21:22

2 Answers 2

23

The following command changes the system's default editor:

sudo update-alternatives --config editor

You will get prompted with the available editors, select vim from the list.

6

The link that you followed was unfortunately generated by ChatGPT, and it demonstrates some of the issues with AI-generated content (and why we ban AI-generated answers here on Ask Ubuntu) see footnote. It's surprising, and a bit disappointing, to see a reputable hosting provider using ChatGPT-generated articles.

The reality is that there are, historically, many ways to choose an editor, and different applications use different methods. ChatGPT came up with a good "guess" (as it often does), but that method only works for some applications, so I'd consider it "wrong" as the answer to an article about, "How to set the default text editor in Linux". The correct answer would be something more like, "It depends on the application".

For instance:

  • crontab -e uses select-editor to allow the user to select their preferred editor the first time it is run. The EDITOR and VISUAL environment variables will override this selection, however, if either is present.

  • Conversely, git defaults to using vi, which can be overridden by the presence of an EDITOR or VISUAL environment variable, which can in turn also be overridden by the core.editor Git configuration.

  • visudo can use the EDITOR and VISUAL variables, but only (if I'm reading man visudo correctly) if the editor or env_editor settings are configured in sudoers. By default, in the absence of these settings and variables, it defaults to /usr/bin/editor. Under Ubuntu, with the alternatives system, /usr/bin/editor is a symlink to /etc/alternatives/editor, which in turn is a symlink to your preferred editor (nano by default).

    As mentioned in the other existing answer, this symlink (as with any in the alternatives system) can be updated using the update-alternatives command.

And these methods for choosing a default editor can even vary from distribution-to-distribution, and some defaults can be compiled into the applications.


Footnote

Certainly, it's easy for a human to make similar mistakes (and I can't promise that mine isn't error-free), but ChatGPT (currently, at least) seems to have a knack for delivering answers that don't quite answer the question that was asked (as in this case), or worse, completely make up settings that don't even exist.

You must log in to answer this question.

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