Clarification on using .vimrc file with sudo

Hi,

I was just wanted to confirm that what I’ve done won’t cause some issue which I don’t know about.

So I’ve started learning about vim recently and have configured my ~/.vimrc. I noticed soon after that when I ran a command with sudo that my preferred settings weren’t working.

After some googling I found out that root has it’s on settings and files etc and the solution to make my .vimrc settings run with sudo is to run sudo -E vim [filename]. I didn’t want to have to always use the -E flag every time I ran sudo with vim so I searched a bit more and found this: sudo cp ~/.vimrc /root/.vimrc.

Is there an issue with copying my .vimrc to root’s .vimrc?

/etc/vimrc is where the system wide settings are kept, maybe I should do sudo cp ~/.vimrc /etc/vimrc ?

Nope, you just have two copies now (so if you make a change you want in both then you have to keep them in sync).

That file is installed by the vim-runtime package so you’ll have to merge any changes when that package is updated. If you can, it’s normally better to set configuration outside of packaged files.

2 Likes

I don’t know if it’s safe to copy files to /root but what I’d do is to create an alias for sudo -E vim

alias svim="sudo -E vim"

in my .zshrc (or bashrc)

That way, svim file.txt will open the file as sudo.

(I’m assuming that sudo -E vim works fine for you.)

Edit:

This pops up when doing a web search. I haven’t tried this; but looks promising.

2 Likes

Thank you both for your replies.

I was planning to keep them both in sync.

So with this alias in the future using sudo with vim I would write it as svim and it would work as sudo -E vim ?

That sounds like a very good way.

Yes it did work it’s just extra typing which I didn’t want

1 Like

Yes. (Again, I am assuming that the command sudo -E vim is working.)

Thank you very much. :nerd_face:

2 Likes

Okay I’ve messed up a bit.

In the timeshift user settings I have excluded all root files so when I go back to my last snapshot the ~/.vimrc which I copied to /root/.vimrc is still there.

two questions.

  1. Is it really a problem to be keeping my ~/.vimrc and `/root/.vimrc in sync together?

  2. regarding timeshift user settings I have my home/user to include only hidden files which is what I want. But for root should I include only hidden files or include all files?

No.

Root shouldn’t have many files, so depends what you want. I’d probably not be using Timeshift for anything other than OS files (i.e. I wouldn’t want to revert changes to documents and emails if a system update failed).

I was always a bit confused about the user options in timeshift I’m glad that’s been clarified, I suppose it was because I didn’t understand that root is also a User.

If I ran sudo pacman -Rns gvim wouldn’t that remove the ~/.vimrc and also /root/.vimrc?

Then I could reinstall gvim change timeshift settings and start again

@jonathon I know you said it isn’t an issue and I take your word on that, I just thought maybe I could revert the changes with pacman -Rns and try the other solution so I don’t have to keep them both synced

I keep mine in sync through a symbolic link (symlink). The root .vimrc points to my user .vimrc. This way, if I edit the .vimrc file it affects both root and my user.

sudo ln -sf ~/.vimrc /root/.vimrc

2 Likes

@2000 Big thank you to you. I have heard a lot about symbolic links here and there but I didn’t really understand what they are except that they are commands that start with ln and link two things somehow. Your example has made it much clearer for me.

so either I can 1. make an alias in .bashrc to sudo -E vim or 2. use a symbolic link to join the root and home users .vimrc.

  1. alias svim='sudo -E vim'

  2. sudo ln -sf ~/.vimrc /root/.vimrc

Its good there are many ways to do things in Linux, lol its just a matter of finding out how to do them.

1 Like

Why not do both? You may want to edit a file with root privileges under your normal user account.

1 Like

I didn’t get you. If I for example just changed my .bashrc then when I edit a file with my normal user account with sudo it will have my .vimrc settings and without sudo it will have my .vimrc settings

if i put alias svim=‘sudo -E vim’ in .bashrc then with my account i write svim [filename] that should make sudo take the .vimrcsettings. And without sudo of course my settings will be from~/.vimrc`

You’re right if you refer to the .vimrc settings being loaded, of course.

I meant this more in the way of svim file being shorter than sudo vim file :grin: .

1 Like

haha @2000 cheers. For a second you saying

Made me question myself whether I had, actually understood anything at all.

Thanks

2 Likes