LLI Wiki — My Digital Knowledge Base

Change the default editor to vim (systemctl edit, sudo)

Three ways to make systemctl edit and other tools use vim as the default editor.

On Debian or Ubuntu, systemctl edit and other tools open the editor set by the EDITOR variable or by the alternatives system. Three methods select vim.

Method 1: SYSTEMD_EDITOR variable and sudoers

Add the variable to ~/.bashrc:

export SYSTEMD_EDITOR=vim

Allow sudo to keep the variable, with sudo visudo:

Defaults  env_keep += "SYSTEMD_EDITOR"

Start a new bash session, then run sudo systemctl edit <unit> as usual.

Method 2: alternatives system

Install the wanted editor (here vim.gtk3), then register the editor link:

sudo update-alternatives --install "$(which editor)" editor "$(which vim.gtk3)" 15

Choose the default editor:

sudo update-alternatives --config editor
There are 7 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/vim.gtk3    50        auto mode
  1            /bin/ed             -100       manual mode
* 2            /bin/nano            40        manual mode
  3            /usr/bin/code        0         manual mode
  4            /usr/bin/gedit       5         manual mode
  5            /usr/bin/vim.basic   30        manual mode
  6            /usr/bin/vim.gtk3    50        manual mode
  7            /usr/bin/vim.tiny    15        manual mode
Press <enter> to keep the current choice[*], or type selection number: 6

Method 3: set the variable on the command

For a single call:

sudo EDITOR=vim systemctl edit <foo>
Mastodon LLIMastodon SocialFosstodon