DEV Community

loading...

sudo vim

Serhat Teker
Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:js:go |
Originally published at tech.serhatteker.com on ・3 min read

You need to edit your file as root. Maybe you need to change default keyboard layout from "en" to "de" with editing /etc/default/keyboard.

OK, you opened the file with your favorite text editor -aka. vim, started to edit but realised that something's wrong. Your mappings not working. Even your alias not working for neovim -eg. alias vi=nvim, it opened the file "legacy" vim. You may come up a solution with another alias like:

alias svi=sudo nvim

But it yields another error:

E484: Can't open file $MYVIMRC

What? It is where it should to be and there is no problem with normal user editing. Problem is when you edit with sudo it uses root's paths and settings. So if you don't have a copy your .vimrc file in the root's $HOME there is no change getting it. Or is there?

Sure there is; show your .vimrc path to vim:

$ svi -u ~/.config/nvim/init.vim

Details for -u flag from manpage of nvim:

-u {vimrc}  The file {vimrc} is read for initializations.  Most other
        initializations are skipped; see |initialization|.  This can
        be used to start Vim in a special mode, with special
        mappings and settings.  A shell alias can be used to make
        this easy to use.  For example: >
            alias vimc vim -u ~/.config/nvim/c_init.vim !*

It seems OK but it won't help with your plugins. Another solution:

$ sudo -E nvim /etc/some_conf_file

What does -E do? From manpage:

-E    The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables.  The security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.

Hence, you should use this option with care, and don't use it hastily as an alias. Actually do not use it at all.

After some research I find another "vim-ic" solution:

" Editing a protected file as 'sudo'
cmap W w !sudo tee % >/dev/null<CR>

So when you want to save the changes use :W instead of :w, vim will prompt for a sudo password, enter your sudo password and hit Enter. Good.

:w !sudo tee % >/dev/null
sudo: tty present and no askpass program specified

shell returned 1

You thought this is the solution but, no.

Granting the user to use that command without prompting for password should resolve the problem:

First;

$ sudo visudo

Then edit that file to add to the very end:

username ALL = NOPASSWD: /fullpath/to/command, /fullpath/to/othercommand

" eg
" sam ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop

This will allow user sam to sudo poweroff, start and stop without being prompted for password. But this is not a work around that I like to use for some possbile security issues as well.

OK then, what would be the "ultimate" solution for this problem, answer: sudoedit

Use sudoedit instead of sudo vim. Make sure your $EDITOR environment variable is set to vim or nvim. Probably already is, or vim is the default; you can set it in your .profile or .bashrc/.zshrc file:

export VISUAL=nvim
export EDITOR="$VISUAL"

Now you can edit the file with usual configs as usual:

$ sudoedit /etc/some_conf_file

All done.

Discussion (0)