4

I am trying to make sure that my neovim configuration gets used for all users. Basically I want my neovim configuration at ~/.config/nvim to be used on using the nvim command with sudo. Hence I copied ~/.config/nvim to /etc/xdg/nvim.

The problem is that I am using astronvim and it uses an init.lua file and not init.vim. According to this I need a sysinit.vim file. Hence I created a sysinit.vim file but I am not sure about how I can import my Lua config file within sysinit.vim. How can make this happen?

AdminBee
  • 21,637
  • 21
  • 47
  • 71
  • 1
    Instead of using `sudo nvim /some/file`, try using `EDITOR=nvim sudoedit /some/file` – muru Feb 17 '23 at 06:21
  • I'm seconding muru's comment about using `sudoedit` with `EDITOR` (or `VISUAL` or `SUDO_EDITOR`) set to `nvim`. – Kusalananda Feb 17 '23 at 07:05
  • Does this answer your question? [Source user config when becoming root](https://unix.stackexchange.com/questions/632423/source-user-config-when-becoming-root) – Panki Feb 17 '23 at 07:30
  • The linked dupe only works for `sudo`, but from your question I guess that's what you want – Panki Feb 17 '23 at 07:31

1 Answers1

1

If you just want to use the nvim command with sudo, keep ~/.config/nvim, you don't need create other config files.

Run:

sudo -E nvim /path/to/file

Or:

EDITOR=nvim sudoedit /path/to/file
EDITOR=nvim sudo -e /path/to/file

From man sudo:

     -E, --preserve-env
                 Indicates to the security policy that the user wishes to preserve their existing environment variables.
                 The security policy may return an error if the user does not have permission to preserve the environment.


     -e, --edit  Edit one or more files instead of running a command.  In lieu of a path name, the string "sudoedit" is
                 used when consulting the security policy.  If the user is authorized by the policy, the following steps
                 are taken:

                 1.   Temporary copies are made of the files to be edited with the owner set to the invoking user.

                 2.   The editor specified by the policy is run to edit the temporary files.  The sudoers policy uses the
                      SUDO_EDITOR, VISUAL and EDITOR environment variables (in that order).  If none of SUDO_EDITOR, VISUAL
                      or EDITOR are set, the first program listed in the editor sudoers(5) option is used.

                 3.   If they have been modified, the temporary files are copied back to their original location and the
                      temporary versions are removed.

You can just run sudoedit /path/to/file if Neovim is configured as EDITOR in your shell config file. Example:

# .bashrc
EDITOR=nvim
Cyrus Yip
  • 191
  • 1
  • 6