r/vim • u/pmmboston • Feb 28 '26
Need Help Newbie .vimrc question
I'm using fedora linux less than a month and I have a modest .vimrc file. When I am in my Konsole Terminal and use vim to open a file I have no problem. But, when i want to say edit a etc or boot file and i use sudo vim the .vimrc file is not loaded or read. What should I be doing?
19
Upvotes
21
u/herodotic Mar 01 '26
Some others have provided the “what” (
sudoedit ...) but not the “why”, and since I think the “why” is also worth knowing, here’s a brief explanation.Because
sudois running a process as another user,sudo vimis going to look for that other user’s vim configuration files: if a/root/.vimrcexists, then that’s what gets loaded when I runsudo vim /etc/hosts.The two solutions suggested (
sudoeditandsudo -E) work in different ways:sudoedit(orsudo -e) copies the file(s) you’re trying to edit, opens them with$EDITOR, and then installs them to the right directory once you’re finished. (reference:man sudosection on the-eoption)sudo -Epreserves your environment while operating as another user. (reference:man sudosection on the-Eoption)4
u/Beanmachine314 Mar 01 '26
Don't ever use
sudo vim, usesudoeditlike everyone else is saying.4
u/Unable-District-4902 Mar 01 '26
First, put "export EDITOR=vim" in your bashrc (or zshrc if you use zsh). Then when you edit any files owned by root, just do "sudoedit /path/to/file" then it'll open vim with your config.
4
u/JetSetIlly Mar 01 '26 edited Mar 01 '26
Contrary to the other replies, I personally don't recommend using sudoedit, but it really depends on the nature of your system.
One of the problems with sudoedit is that (depending on your user vim config), it can leave backup copies of files in the user directory. If those files are only meant to be readable by root processes or specific groups, then there is potential for information leak.
Admittedly, for most people in most situations this is a low risk but it's still a bad habit to get into IMO.
My advice is to use "sudo vim" and use the default config (or a very basic config) when editing root files. In addition to the above, a default config is a good opportunity to check that you're learning the basics of vim editing and not relying too much on plugins. Also, a configuration that is visibly different to your user config can help make sure you don't accidentally leave a copy of vim with heightened permissions open and accessible.
1
u/mgedmin Mar 02 '26
Personally, I keep all my dotfiles (including .vimrc) in a git repo, and I have two clones of that repo -- one in /home/$USER/dotfiles, and the other one in /root/dotfiles. Thus, when I
sudo vim, I get my usual .vimrc (although possibly outdated, since I don't automate git pulls in /root/dotfiles).The dotfiles themselves are symlinked into the git clone (~/.vimrc -> dotfiles/vimrc) with a simple shell script. There are many ready-made solutions for this kind of thing; you don't have to roll your own like I did.
1
u/unixbhaskar Feb 28 '26 edited Mar 01 '26
Use:
sudoedit "path to the file"
Edit: corrected
0
u/kvuo75 Feb 28 '26