WRITELOOP

SUDOEDIT: FIXING ERROR 'EDITING FILES IN A WRITABLE DIRECTORY IS NOT PERMITTED'

sudoedit is useful when you need to edit a file like if you were root, but you want to keep your editor configuration and plugins (e.g. vim, neovim). Here is how to fix this error.

2022 September 16

Suppose you want to edit a file owned by root, and you have sudo privileges.

You could do that this way:

$ sudo vim /etc/fstab.

Doing that, you will open vim using the root user configuration, so you will not be able to use the plugins and customizations you’ve done to your editor.

But there is an alternative way to do that.

First, you have to open a terminal and set an environment variable with the editor path. Then, you can open the file using sudoedit:

$ export EDITOR=/usr/bin/nvim  # change here to the path to your default editor. Mine is neovim, but you can use nano, vim, gedit, code, etc...
$ sudoedit /etc/fstab

Now, you will be able to edit this file as root, but keeping all the customizations you’ve done to neovim - it will open as your user but will save as root.

But in some cases, sudoedit may fail with the error below:

$ sudoedit /etc/fstab
sudoedit: /etc/fstab: editing files in a writable directory is not permitted

To fix that, you must run this command:

sudo visudo

Then, at the beginning of the file, on the “Defaults” section, add another line with the following contents:

Defaults        !sudoedit_checkdir

Save the file and then run sudoedit again. It will work from now on without this error message bothering you again.