2

I'm having an issue with Neovim on a Raspberry Pi coding with Python.

I have installed it by sudo apt-get install Neovim, and it works using just the nvim command on the command line. For some reason, I can create a file with nvim filename.py, but it will end as a read-only file.

If I run Neovim as sudo nvim instead, I can write to the file, but my init.vim file is not being loaded.

I have created my init.vim here: /home/pi/.config/nvim/init.vim

Does it have to be placed elsewhere, or can I make some kind of link to it?

I have also tried giving filename.py writing permission with sudo chmod a+w filename.py, but that leads me to an error code "E509" when trying to save by :wq. It will save with :wq! though.

3
  • 1
    I am not familiar with NeoVim (I just use vim), It sounds like you start in Read Only mode. Once you opened your file in vim, could you try: :set noro? That should disable read only mode. If that works, you are setting ro somewhere in your init.vim.
    – Johan
    Jan 14, 2019 at 16:06
  • I am not sure how init.vim is located, I assume it uses $HOME. Could you show me the output of this command? sudo env | grep HOME
    – Johan
    Jan 14, 2019 at 16:14
  • 1
    It's unclear how editing a file with nvim filename.py cause the file to be read-only, unless you mean you can't save the file due to permissions on the current directory.
    – Kusalananda
    Feb 6, 2023 at 7:08

4 Answers 4

7

You should use sudoedit or sudo -e instead of sudo nvim. You can choose your editor by setting the SUDO_EDITOR, VISUAL, or EDITOR environment variable.

When using sudoedit or sudo -e, a copy of the given file is edited as the user invoking the command, which means that the initialisation files associated with the editor (~/.config/nvim/init.vim in the case of Neovim) will be read from that user's account, not from the root user's account.

0
2

For those who don't have a sudoedit command. We can just make an alias using sudo :)

export SUDO_EDITOR="nvim"
alias "sudoedit"='function _sudoedit(){sudo -e "$1";};_sudoedit'

Now doing something like sudoedit foo will run sudo -e foo, and use nvim as the editor, with all your plugins and configurations.

1
  • I needed to add a space between the curly bracket and sudo ( { sudo ) for it to work in bash.
    – Marcelo
    Jan 6 at 5:53
-1

To do what you would like to do you should copy your init.vim or .vimrc file to /root/.config/nvim.

You should also remove the plugin section because it will cause problems when in root.

-1

I had the same problem, when I use sudo, I lost the neovim configuration, this worked for me:

sudo -Es nvim README.md

You must log in to answer this question.

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