1. Overview

Configuring and customizing a Linux environment is a common task for many. One aspect that might need adjustment is the default text editor for editing unit files in systemd. To clarify, systemd is a system and service manager for Linux operating systems. It plays an important role in the initialization of the system.

In this tutorial, we’ll explore various methods to change the default text editor for unit files. Moreover, we’ll discuss the advantages and disadvantages of different text editors for that role.

2. Understanding Text Editors

Before we dive into changing the default editor, let’s briefly discuss some text editors available in Linux and their characteristics.

In particular, **we’ll highlight the importance of the most common text editors such as nano, vim, and emacs**.

2.1. nano Text Editor

Let’s list a couple of advantages of using nano:

  • simple and user-friendly interface
  • ideal for beginners

On the other hand, nano lacks some advanced features present in other editors.

2.2. vim Text Editor

Now, let’s explore some of the pros of using vim:

  • powerful and feature-rich
  • modal interface for efficient text manipulation

Nevertheless, one of the cons of using vim is that it has a steeper learning curve for new users.

2.3. emacs Text Editor

Next, let’s go through some advantages in favor of emacs:

  • extremely extensible and customizable
  • offers a wide range of functionalities

Although emacs offers significant advantages, one of its drawbacks is that it can be overwhelming for beginners.

Moreover, the differences between the three editors aren’t huge and we can meet our goals by using any of them. However, the choice hinges on our familiarity with one over the other when deciding which to designate as their primary text editor.

In the upcoming sections, we’ll examine various methods specifically tailored to change the default editor for unit files, as altering the general default editor is the key to influencing the editor used in systemd unit file edits.

3. Unit Editing and SYSTEMD_EDITOR

In Linux, the SYSTEMD_EDITOR environment variable determines the default text editor. Additionally, systemd uses this variable when opening a text editor for unit file editing by default.

The command employed for these edits is systemctl edit:

$ systemctl edit ...

If the SYSTEMD_EDITOR environment variable isn’t set, systemd falls back to using the VISUAL environment variable. However, if both SYSTEMD_EDITOR and VISUAL are unset, systemd then uses the EDITOR environment variable.

3.1. Theoretical Understanding

Here, we’ll check how to edit the Bash configuration file ~/.bashrc. In particular, ~/.bashrc refers to the specific user configuration file, located in their home directory.

Accordingly, we can modify this file to meet certain requirements:

  • customize our shell environment
  • set aliases
  • define functions
  • configure various aspects of the Bash shell’s behavior

Now, as we theoretically understood the ~/.bashrc file, we’ll move to its practical modifications.

3.2. Configuring bashrc

Let’s continue by modifying our user-specific Bash configuration file ~/.bashrc to set the SYSTEMD_EDITOR variable:

$ echo "export SYSTEMD_EDITOR=vim" >> ~/.bashrc
$ source ~/.bashrc

In the above snippet, we use the echo command and the redirection >>  operator to append the export statement to the ~/.bashrc file. This way, we set the SYSTEMD_EDITOR variable to vim. Moreover, we then use the source command to update ~./bashrc and apply the changes immediately.

3.3. Configuring /etc/sudoers File

Since root is the user commonly granted access to change unit files, most users making such changes require sudo. Therefore, when we’re not logged in as the root user, we use the sudo command to obtain superuser privileges.

Thus, we may need to open the /etc/sudoers file for safe editing with visudo:

$ sudo visudo

When we open the sudoers file, we just need to add one line to it:

Defaults        env_keep += "SYSTEMD_EDITOR"

In essence, this line ensures that the SYSTEMD_EDITOR environment variable is preserved when running commands with sudo. Accordingly,  it sets Vim as the editor for systemd service files with elevated privileges.

4. Using update-alternatives

Another approach involves using the update-alternatives system, which enables us to choose the default editor.

First, we need to ensure that we set up the application that we want to use as our primary text editor:

$ sudo update-alternatives --install /usr/bin/editor editor /usr/bin/vim.gtk3 20

In particular, we use the –install option with the update-alternatives command to create or update symbolic links determining default commands for various programs on a Unix-like system.

Now, let’s go through the above command in-depth:

  • editor is the name of the link group
  • /usr/bin/editor is the symbolic link that will be created or updated
  • /usr/bin/vim.gtk3 is the alternative that we add to the link group
  • 20 is the priority; it’s a number that determines the priority of this alternative when it’s auto-selected, in which the higher the number, the higher the priority

Next, we use the –config option to select our preferred editor:

$ sudo update-alternatives --config editor

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.gtk3    20        manual mode

Press <enter> to keep the current choice[*], or type selection number:

Since we want to select vim as our primary editor, we followed the above instructions from the output and type the number 4 to select the package we installed:

update-alternatives: using /usr/bin/vim.gtk3 to provide /usr/bin/editor (editor) in manual mode

Here, a confirmation message is displayed as an output.

5. Setting at Runtime

In the previous two methods, we were aiming for a permanent change when selecting our primary text editor. However, we can also set our preferred text editor dynamically at runtime.

For example, let’s suppose we configured nano as our primary text editor. Nevertheless, for editing a specific file called my-service, we may need to override the change and use vim instead of nano.

For a temporary change, we can set SYSTEMD_EDITOR directly during runtime:

$ sudo SYSTEMD_EDITOR=vim systemctl edit my-service

In the above snippet, we set the SYSTEMD_EDITOR environment variable to use vim for this specific command only.

Consequently, this method offers a convenient and temporary solution to override the default editor for a specific systemd unit file edit without modifying the global Bash configuration. Particularly, it enables users to specify an editor for a single command, avoiding permanent changes that might affect the entire system or user environment.

This method enables us to override the default editor for individual commands, including the use of systemctl edit, offering a flexible way to adjust unit file promptly based on specific tasks or troubleshooting needs.

6. Configuring Aliases

An alias is a user-defined shortcut or abbreviation for longer commands. In addition, it enables us to create simple and convenient abbreviations for commonly used or complex commands.

Aliases have various advantages:

  • save time
  • reduce typing effort
  • make our command-line experience more efficient

Let’s see how to set an alias to configure our preferred text editor:

$ alias sc='sudo SYSTEMD_EDITOR=/bin/vi /usr/bin/systemctl'
$ sc edit service-name

In the above command, we use the word alias to configure a new alias. Next, we define its name as sc. Then, as we did in previous sections, we set the correct paths to SYSTEMD_EDITOR. Moreover, /usr/bin/systemctl indicates the location of the systemctl executable on the system. This ensures that the correct systemctl command is invoked when we use the sc alias.

Finally, we use the sc alias to edit the systemd service unit file named service-name.

7.  Conclusion

In this article, we explored configuring the default text editor for systemd unit file edits in Linux.

Moreover, we checked several ways to set our preferred editor:

  • user-specific configurations
  • system-wide adjustments
  • runtime changes
  • convenient shell aliases

Furthermore, from the simplicity of nano to the feature-rich vim and the extensibility of emacs, we can select an editor that aligns with our proficiency and workflow.