3

I am using Kubuntu 20.04.

When I run sudoedit /etc/fstab, VS Code opens to a blank document and the CLI immediately returns (see details below).

If I run export SUDO_EDITOR=nano, the document opens in the nano editor with the contents of /etc/fstab as expected.

If I run export SUDO_EDITOR=/snap/bin/code, it once again opens VS Code with a blank document.

What am I doing wrong? Or is this a bug?


kevin@kevcoder00 ~ $ echo $VISUAL

kevin@kevcoder00 ~ $ echo $SUDO_EDITOR

kevin@kevcoder00 ~ $ echo $EDITOR /snap/bin/code kevin@kevcoder00 ~ $ sudoedit /etc/fstab [sudo] password for kevin: sudoedit: /etc/fstab unchanged

Stephen Kitt
  • 434,908
kevcoder
  • 485
  • 1
  • 4
  • 13

2 Answers2

13

You need to tell the editor to wait:

SUDO_EDITOR="/snap/bin/code --wait" sudoedit /etc/fstab

Without that option, VS Code forks, or notifies an already-running instance, and control immediately returns to sudoedit. The latter sees that nothing has changed and deletes the temporary copy that is used for editing purposes. (Snap might contribute to the effect, but VS Code on its own requires this.)

See also How to properly edit system files (as root) in GUI (and CLI) in Gnu/Linux?

Stephen Kitt
  • 434,908
2

Alternatively, you may try (maybe an overkill solution in your case) my script:

https://github.com/burianvlastimil/sudoedit-enhanced


I worked hard to document it well, and the latest, very recent, revision is stable.

In any case, Stephen Kitt's solution may be fine.


But, sudoedit does not work well with, specifically, VS Code, unfortunately - as I was asked for details, from comment I quote:

@StephenKitt Sure. Here it is from my testing one year ago approx. The only way to edit as root with code was at that time via sudo code /path/to/file. Did something change? I would surely like to know.