90

I'm using CentOS and created a .vimrc file in my /home directory. I tested it out by creating a txt file and yes, that worked fine. Now, I have my project files in my /srv directory with SELinux turned on. I tried opening a file: vim README.txt and yes, my .vimrc settings are still being applied.

Now, since I'm in the /srv directory, simply doing vim means that my file is read only. So, I do sudo vim README.txt in order to be able to edit files. Now, the problem lies that once I do sudo, none of my .vimrc settings are applied. I tried creating a copy of .vimrc in the /srv folder but that didn't work either.

How do I apply .vimrc settings while using sudo?

noblerare
  • 10,277
  • 23
  • 78
  • 140
  • 1
    I would just add a brief explanation: if you execute `sudo`, you switch yourself to the `root` user, so `$HOME` becomes the `/root` directory (unless the `-E` option is used). That means `vim` will load the configuration in `/root/.vimrc`. `vim` is not configured to load the configuration from the current directory by default, that's why it didn't load it from the `/srv` directory. – David Ferenczy Rogožan Aug 05 '16 at 14:52
  • Sorry for digging up this old question but maybe it helps someone: For me, `vi` was set as an alias for `vim` for my named-user but not for the root user. No wonder none of the below answers helped; I kept using `vi`. – evilolive Apr 27 '22 at 13:50

4 Answers4

140

Use sudoedit instead of sudo vim. You should be doing that anyway. Make sure your EDITOR environment variable is set to vim (probably already is, or vim is the default; you can set it in your .profile analog if need be).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 7
    Perfect. Thank you! One conceptual question: why should I be using `sudoedit` over `sudo vim`? – noblerare Jan 31 '14 at 19:40
  • 23
    @noblerare for security. Probably doesn't matter for your own system, but it is very important for a system that many users with `sudoers` privileges. You can run any shell command from `vim`, so with `sudo vim` you could do anything! `sudoedit` creates a temporary copy of the file and doesn't give such privileges. `man sudoedit` and `man visudo` – Explosion Pills Jan 31 '14 at 19:42
  • 3
    Also, `$ sudo -e filename`. – romainl Feb 01 '14 at 10:47
  • 8
    Note that the actual file isn't updated until you exit - not good if you like to save & test your changes without closing vim. – Roger Dueck Jul 07 '20 at 21:36
63

As shown here, you can use the following:

sudo -E vim README.txt

From the man page:

-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.

The accepted answer is the most secure. But this one is more flexible as I can use sudo -E operation with any operation, I don't have to configure anything else beforehand.

leetNightshade
  • 2,673
  • 2
  • 36
  • 47
  • @Claudio do you have any more details than that, as to your specific setup? `sudo -E *` should work for any command. Are you sure the .vimrc you want to use is set for the currently running user that you're logged into? Are you sure you didn't accidentally switch to another user and using their .vimrc or lack of one, instead of the one you want to use? – leetNightshade Dec 10 '15 at 18:13
  • Tried again. OS: CentOS 7. If I run `vim *` `.vimrc` is loaded. If I run `sudoedit` it is loaded too. But if I run `sudo -E vim *` it doesn't. – Claudio Dec 11 '15 at 08:05
  • @Claudio What is the * for? I'm not saying you shouldn't use it, I just don't know what it is for. Also, I use `sudo -E vim` without the * and that works for me. – still_dreaming_1 Feb 28 '17 at 18:24
  • 1
    @still_dreaming_1 I thought he meant whatever file he was trying to edit (replace * with the filename), I assumed he didn't actually type that. Good question. – leetNightshade Mar 10 '17 at 06:16
  • this solution also gives you flexibility to use extensions such as CTRL+P, unlike the sudoedit solution – Kaa May 02 '21 at 05:24
  • Indeed the most practical answer. `sudoedit` is good when on a server, but its security when editing my home machine's configs is a bit over the top. – Michael Ekoka Jan 25 '22 at 18:37
19

/root/.vimrc is the working directory of sudo vim.

You need copy your .vimrc file from /home/ec2-user/.vimrc to /root/.vimrc

James He
  • 494
  • 3
  • 6
3

The presented solutions in the other responses work but are not very practical, as you have to enter you password every time you want to edit a file.

I usually have a tmux session open within which I am rooted via sudo su, so I enter my password once at the beginning of the session and can then work for hours without having to enter it again.

I worked around the issue presented here by creating the following symbolic links : sudo su ln -s /home/MY-USER-NAME/.vimrc .vimrc ln -s /home/MY-USER-NAME/.vim .vim

You might need to remove the /root/.vim/ directory first.

I hope this helps

Alexandre Bourlier
  • 3,972
  • 4
  • 44
  • 76
  • Unless I'm mistaken, you don't need `sudo` for `su`. Just `su` and enter your password. If you want the benefits of a superuser tmux session (i.e. avoid typing the password repeatedly), while also keeping your normal user environment, and without having to create symlinks in the root home directory, use `sudo -E` when you start your tmux session, as suggested by one of the previous answers: `sudo -E tmux new -s foo`. – Michael Ekoka Jan 26 '22 at 16:05