2

I have an alias defined alias svi='sudo /usr/bin/vim -i /home/jeff/.viminfo -u /home/jeff/.vimrc' but when I type svi myfile I get

Error detected while processing /home/jeff/.vimrc:
line 1:
E117: Unknown function: pathogen#infect

It seems I need to force vim to scan my .vim directory before executing call pathogen#infect() How do I do that, and in general, how can I make sure that when I run sudo vim I get the same environment that I would have by just running vim?

4
  • 1
    As a side note, if the reason you want sudo vim is to be able to edit root-owned files, you may look at Tim Pope's vim-eunuch plugin. It has a SudoWrite command which will allow you to overwrite a priviliged file from Vim started without privileges.
    – taketwo
    Jun 20, 2017 at 14:59
  • thanks I will try vim-eunuch, but I think understanding how to control initialization is still a worthy goal.
    – Jeff
    Jun 20, 2017 at 17:44
  • Have you installed Pathogen as root? My thought is that it is looking for the root user to have Pathogen installed. Perhaps you can give it the full path.
    – Shadoath
    Jun 21, 2017 at 0:07
  • @Shadoath it turns out you were sort of on the right track, but I don't want to modify root's environment (in part because there are a half-dozen devops folk who need to do things as root on the system). I just needed the secret sauce for getting vim to look at my own .vim directory. Check out my answer below.
    – Jeff
    Oct 15, 2017 at 22:30

2 Answers 2

4

how can I get my normal vim initialization when I execute sudo vim?

By using $ sudo -e filename instead of $ sudo vim filename.

That command will:

  1. ask for your password (if the sudoers security policy requires it);
  2. make a copy of filename in /tmp/;
  3. open it as you with the first editor found by checking the following, in order: $SUDO_EDITOR, $VISUAL, $EDITOR, and finally the list of editors specified in /etc/sudoers;
  4. copy it back to the original filename on write.

Refer to man sudo and man sudoers for more information.

2
  • On most systems you can also do sudoedit instead of sudo -e.
    – Heptite
    Aug 23, 2017 at 23:15
  • 1
    As an added benefit, this is safer, since $EDITOR is run without root privileges.
    – 8bittree
    Sep 5, 2017 at 23:12
1

While I intend to accept @romainl's answer as the preferred solution (after he tweaks it a little), I took another crack at researching a solution to the error I was getting because I hate it when computers get the better of me.

It turns out the error has to do with how the vim runtimepath option resolves when running as root. So to correct the error, I changed my alias slightly to alias svi='sudo /usr/bin/vim -i /home/jeff/.viminfo -u /home/jeff/.svimrc' and put the following three lines into /home/jeff/.svimrc

set runtimepath=$HOME/.vim,$VIM/vimfiles,$VIMRUNTIME,$VIM/vimfiles/after,$HOME/.vim/after,/home/jeff/.vim
source /etc/vimrc
source /home/jeff/.vimrc

I no longer get the error and my custom stuff seems to be working (so far). But if I run into any other glitches, I will peruse :help initialization some more to see what else should go into my .svimrc file....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .