Vim Trick: Use your user’s .vimrc even when you sudo

I have a huuuuuuuge .vimrc configuration file. When I need to edit an /etc file, for example, I use sudo.

like: sudo vim /etc/passwd

But, using sudo I loose my user’s ~/.vimrc file, as vim uses it’s default.

I have googled for a solution for having my user’s .vimrc file even when I use sudo, and so, I have found this solution:

$ sudo vim -u ~/.vimrc /etc/some_file

The -u arguments asks vim to load the file following it.

To make things easier, I have created this usefull function:

  • svim () { sudo vim -u /home/razvan/.vimrc @$ } ;

To make this function permanent, add the line to your ~/.bashrc file.

Now, instead of using that long sudo command, just type svim /path/to/file.

Tagged with: , , , , , ,
Posted in The Linux and Unix Articles!
One comment on “Vim Trick: Use your user’s .vimrc even when you sudo
  1. RandomLuser says:

    sudoedit is standard and much better (more secure, more portable, etc.) than this function.

Leave a Reply

Your email address will not be published. Required fields are marked *




Subscribe to get the latest Linux news and how to guides directly on your e-mail!