After opening vim as normal user. Is it possible to open a new file as sudo user? like for example in :e file.

  • You'll have to start vim with sudo for it to have root privileges. – Tumbler41 Jan 24 '18 at 15:13
  • @Tumbler41 so it's not possible? – yukashima huksay Jan 24 '18 at 15:16

One workaround is to open the file normally:

:e file

And then use the sudo tee trick when writing the file:

:w !sudo tee %

If you don't have read permission on the file, you're going to need to use sudo to get the file contents into Vim, too.

" (If necessary) Create a new, empty buffer

" Read file into the empty buffer
:r !sudo cat file

" Delete first (empty) line

" Do edits...

" Write file
:w !sudo tee file

" Discard buffer and file contents

Personally, if I haven't already started making changes to the file, I'd just quit vim and then use sudoedit:

sudoedit file

I use the SudoEdit plugin, which defines the commands SudoRead and SudoWrite, which allow me to read and write with sudo permissions a file I didn't think to open with sudoedit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.