4 Ways to Change the Default Text Editor on Linux


Don’t like the default text editor on your Linux distro and want to change it to something else? It’s pretty simple to do with a few short commands. I’ll walk you through several ways you can do it, no matter which Linux version you’re using.


For this guide, I’m using Ubuntu 24.04 LTS. If you’re using a different distro, you can still follow along as most if not all the commands are the same.



Checking Current Default Text Editor

Before changing the default text editor, it’s good to know the current default one set on your Linux distro. To do that, you can check the $EDITOR variable by running this command:

echo $EDITOR

However, if your $EDITOR variable is not set, you might not get any output in return. If that’s the case, you can also check the default editor using this command:

sudo visudo

Running the visudo command to check the default text editor on Linux.

The file was opened in the nano editor. So, this is my default editor.

Changing the Default Text Editor

Now that you know your current default editor, let’s explore how you can change it.

Using select-editor

The select-editor command lets you select a per-user editor. It selects your preferred sensible-editor. Run the command:

select-editor

You’ll see several editors listed next to a number. Type in the number for the default editor you want and hit Enter.


Changing the default editor on Linux using the select-editor command.

For demonstration, I’m entering 2 to change the default editor to Vim. To check if it was successful, run:

cat ~/.selected_editor

Checking the selected_editor file to find the current default editor.

It should show you the editor you just selected.

Using the update-alternatives Command

The update-alternatives command lets you maintain symbolic links that determine default commands. You can use this to change default behaviors such as which editor to open for a system call. Unlike the select-editor tool, this is a system-wide selector. To use this command for changing the editor, run:

sudo update-alternatives --config editor


Then select the number for your preferred editor and press Enter.

Changing the default editor on Linux using the update-alternatives command.

Now, to check if you changed it successfully, run the below command:

ls -l /usr/bin/editor /etc/alternatives/editor

Checking if update-alternatives could change the default editor.

In my case, the editor was changed from nano to Vim successfully.

Editing the EDITOR Environment Variable

If the above methods didn’t work for you, or your distro doesn’t support them, then consider configuring the necessary environment variables, which I’ll cover in this and the next method.


First, open your shell configuration file in a text editor. Since I’m using Bash on Ubuntu, I’ll be working with the .bashrc file. In other cases, this could be the bash_profile file or another file depending on your shell. To open the file in nano, run:

nano ~/.bashrc

Then add this line to the end of the file:

export EDITOR=/path/to/your/preferred/editor

For example, for Vim, the line would be as follows:

export EDITOR=/usr/bin/vim

Setting the EDITOR environment variable in the bashrc file.

If you’re not sure where your editor is located, you can write the line like this:

export EDITOR=$(which vim)

This method uses the which command on Linux to locate Vim. Once you’ve written the line, save the file with Ctrl+O and then exit nano using the Ctrl+X buttons.

Here’s a one-liner you can run in your terminal to do the whole thing:

echo "export EDITOR='/usr/bin/vim'" >> ~/.bashrc


After doing this, you need to make the changes take effect by sourcing the file. To do that, run:

source ~/.bashrc

Now test if the variable was set properly by running:

echo $EDITOR

It should show the text editor you had just set and use it as default.

Editing the VISUAL Environment Variable

The VISUAL variable’s editor is capable of advanced terminal functionalities and supports full-screen editors such as nano, emacs, vim, etc. The EDITOR variable’s editor doesn’t have such privileges, which makes them a bit different. You can set the VISUAL variable on your shell configuration file the same way you set the EDITOR variable.

echo "export VISUAL='/path/to/your/preferred/editor'" >> ~/.bashrc

Then run:

source ~/.bashrc

See if you’ve successfully set the VISUAL variable.

echo $VISUAL

Setting the VISUAL environment variable in the bashrc file.

As expected, that worked successfully.


Testing the Changes

The last thing to do is see if you could successfully change your default text editor to your preferred one. You can check this in a few different ways. For example, you can check crontab’s editor. To do so, run:

crontab -e

Running the crontab -e command to check the default editor.

As you can see, my default text editor is now Vim as opposed to nano.

One important thing to know is that there’s an order of precedence among these configurations. So, suppose that you used different methods to define different default editors. Which will be the default editor? To determine that, here’s the order:

  1. Configuring the environment variables will take the highest precedence and override all other settings for the current user.
  2. Software that uses the select-editor command to know the default editor will use the ~/.selected_editor file’s choice as a precedence, again for the current user.
  3. The system-wide configuration using update-alternatives command takes the least precedence and is overridden by the user-specific settings listed above.



Text editing on Linux is one of the most common things to do and doing it in your favorite editor is a bonus. If you don’t like the text editors you have on your operating system, you should also consider trying alternative ones.



Source link

Previous articleM4 Mac mini may have a USB-C problem