[4.13.12-1-ARCH with gnome3 and gdm on Xorg]

I already have set my VISUAL and EDITOR env-vars to vim. Similarly I did try SYSTEMD_EDITOR="vim"; export SYSTEMD_EDITOR in my ~/.bashrc, to no avail.

When modifying unit files in Arch (systemd) via

 $ sudo systemctl edit _unit_ 

I find myself staring at nano. Life is too short and I want vim by all means. How do I do this ?

  • 5
    I have zero patience with Nano and prefer to kill a mosquito with a cannon. I delete Nano and place a symlink to vim in it's place. Done. Commented Oct 31, 2019 at 8:49
  • 2
    "sudo apt remove nano". Remove nano that will make vim as default Commented Nov 22, 2020 at 18:40

4 Answers 4


First method, you can add this line to ~/.bashrc:


And then sudo visudo and add this line:

Defaults  env_keep += "SYSTEMD_EDITOR"

Start new bash session to take effect, then run sudo systemctl edit <foo> as usual.

Second method is use update-alternatives:

Install your desired editor, e.g. vim.gtk3:

$ which editor
editor is /usr/bin/editor
$ sudo update-alternatives --install "$(which editor)" editor "$(which vim.gtk3)" 15

Then choose your desired 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
update-alternatives: using /usr/bin/vim.gtk3 to provide /usr/bin/editor (editor) in manual mode

Third method is direct set the EDITOR on runtime:

sudo EDITOR=vim systemctl edit <foo>

The precedence are first method > third method > second method.

Don't try to set "GUI" editor such as gedit because Why don't gksu/gksudo or launching a graphical application with sudo work with Wayland? and Gedit uses 100% of the CPU while editing files

  • 1
    Thanks 林果皞, but I really meant vim, not gedit.
    – Cbhihe
    Commented Dec 2, 2017 at 22:34
  • @Cbhihe gedit is just an example to proved that even gui editor is working, of course you can use vim with this answer.
    – 林果皞
    Commented Dec 2, 2017 at 22:39
  • 1
    [UPDATE] I edited my answer to add advise don't set gedit.
    – 林果皞
    Commented Jul 5, 2019 at 20:05
  • The third method should have SYSTEMD_EDITOR instead of EDITOR.
    – Qumeric
    Commented May 14, 2021 at 14:34
  • Is there any way to use the sudoedit command for this (that is, have sudoedit launched with my regular, non-root user)?
    – Marcel
    Commented May 31, 2021 at 4:13

You are setting the variables for your own user, but are running the systemctl command as root (sudo). Therefore, the variables you've set for your user are irrelevant.

To fix this, you can either (but go with 1):

  1. Run sudo with -E so it exports the current environment:

    sudo -E systemctl edit _unit_
  2. Add the variable (you only need SYSTEMD_EDITOR for this) to root's ~/.profile:

    export SYSTEMD_EDITOR="/bin/vi" 

    Then run with

    sudo -i systemctl edit _unit_ 

Finally, note that you need to specify the full path to your editor, not just its name. So it's /bin/vi and not vim.


I use a shell alias:

sc='sudo SYSTEMD_EDITOR=/bin/vi /usr/bin/systemctl'

Then just:

sc edit service-name

It's also useful for generally avoiding typing 7 out of the 9 characters of systemctl in cases like restart, etc.


The only ways that worked for me are:

  • A root shell (su or sudo -i), setting SYSTEMD_EDITOR=/usr/bin/vim, then systemctl edit <unit>
  • Setting SYSTEMD_EDITOR, then sudo -E systemctl edit <unit>
  • sudo SYSTEMD_EDITOR=/usr/bin/vim systemctl edit <unit>

I tried setting EDITOR and VISUAL and SYSTEMD_EDITOR for bash and zsh, for user and root, but it only worked from a root shell, setting the variable after sudo, or using sudo with -E.

I also edited sudoers (sudo visudo) to add:
Defaults env_keep += "SYSTEMD_EDITOR"
Still, I found the variable was not listed in sudo systemctl show-environment even though sudo echo $SYSTEMD_EDITOR showed vim.

I set the variable with:
sudo systemctl set-environment SYSTEMD_EDITOR=/usr/bin/vim
And confirmed with sudo systemctl show-environment.

Even after that though, the only ways that work for me are the ones listed at the top.

You must log in to answer this question.

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