DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

sudo vim

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
Enter fullscreen mode Exit fullscreen mode

But it yields another error:

E484: Can't open file $MYVIMRC
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 !*
Enter fullscreen mode Exit fullscreen mode

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

$ sudo -E nvim /etc/some_conf_file
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

$ sudoedit /etc/some_conf_file
Enter fullscreen mode Exit fullscreen mode

All done.

Top comments (0)