6

I'd like to automatically set my Vim preferences on the root account, when I use it (throught a sudo bash or sudo su-like command), without changing the default configuration of the root account (because it's a shared account), and without having to use a lot of keystrokes to do this (I have to switch often).

To complicate things, our home directories are not available when in root mode (they are NFS-mounted, and, for security purposes, root isn't allowed to see inside, as root can be most anybody)

So, for now, I've put a nice little bundle of scripts in a subdirectory under /tmp, which happens to be readable and writable both under my non-root and under the root accounts.

Now, I've written a tiny script to allow me to connect as root with this slightly longer command (it works):

exec sudo bash --init-file /tmp/MY_DIR/MY_BASH_CONFIG_FILE

Inside the MY_BASH_CONFIG_FILE, I put a lot of things, mostly environment variable definitions, along with source /root/.bashrc (in order to build from the usual root config, not from scratch).

Now, after all this exposition, let's get to the heart of the problem: I've looked at Vim's man page, and found a -u FILE option. However, this option will ONLY load the given file, and bypass any other Vim configuration file. Which means that when I tried alias vi=vim -u MY_DIR/MY_TMP_VIMRC, I would get a broken Vim, with arrow keys no longer working as intended, etc.

I've also tried merely naming the file vimrc in my /tmp subdir, and using in the bash config file the line export VIM='MY_TMP_SUBDIR'. However, it still doesn't load the MY_TMP_SUBDIR/vimrc file.

Here are the files loaded when I run Vim as my unprivileged user (as given by the scriptnames Vim command):


  1: /etc/vimrc
  2: /usr/share/vim/vim74/syntax/syntax.vim
  3: /usr/share/vim/vim74/syntax/synload.vim
  4: /usr/share/vim/vim74/syntax/syncolor.vim
  5: /usr/share/vim/vim74/filetype.vim
  6: /usr/share/vim/vimfiles/ftdetect/puppet.vim
  7: /usr/share/vim/vim74/ftplugin.vim

  8: ~/.vimrc

  9: /usr/share/vim/vim74/plugin/getscriptPlugin.vim
 10: /usr/share/vim/vim74/plugin/gzip.vim
 11: /usr/share/vim/vim74/plugin/matchparen.vim
 12: /usr/share/vim/vim74/plugin/netrwPlugin.vim
 13: /usr/share/vim/vim74/plugin/rrhelper.vim
 14: /usr/share/vim/vim74/plugin/spellfile.vim
 15: /usr/share/vim/vim74/plugin/tarPlugin.vim
 16: /usr/share/vim/vim74/plugin/tohtml.vim
 17: /usr/share/vim/vim74/plugin/vimballPlugin.vim
 18: /usr/share/vim/vim74/plugin/zipPlugin.vim

 19: /usr/share/vim/vim74/scripts.vim
 20: /usr/share/vim/vim74/syntax/conf.vim
 21: /usr/share/vim/vim74/ftplugin/conf.vim

Now, here's what I get, running Vim as root:

  1: /etc/vimrc
  2: /usr/share/vim/vim74/syntax/syntax.vim
  3: /usr/share/vim/vim74/syntax/synload.vim
  4: /usr/share/vim/vim74/syntax/syncolor.vim
  5: /usr/share/vim/vim74/filetype.vim
  6: /usr/share/vim/vim74/ftplugin.vim
  7: /usr/share/vim/vim74/plugin/getscriptPlugin.vim
  8: /usr/share/vim/vim74/plugin/gzip.vim
  9: /usr/share/vim/vim74/plugin/matchparen.vim
 10: /usr/share/vim/vim74/plugin/netrwPlugin.vim
 11: /usr/share/vim/vim74/plugin/rrhelper.vim
 12: /usr/share/vim/vim74/plugin/spellfile.vim
 13: /usr/share/vim/vim74/plugin/tarPlugin.vim
 14: /usr/share/vim/vim74/plugin/tohtml.vim
 15: /usr/share/vim/vim74/plugin/vimballPlugin.vim
 16: /usr/share/vim/vim74/plugin/zipPlugin.vim

 17: /usr/share/vim/vim74/scripts.vim

I've spaced some lines for readability: As unprivileged used, I see that the ~/.vimrc file is loaded, but not when I run as root (even though the Vim environment variable is exported, and points to the tmp dir, which is readable, and contains a vimrc file, which is readable too) Also, the end lines (19 to 21 in user mode, or line 17 in root mode) appear only when using Vim on a file (if I type vi, and then ask for the scriptnames on the title screen, I don't get them. However, if I type vi SOMEFILE, then they appear). I'm not sure that's relevant.

Does anyone have an idea as to what I should do to get out of this predicament ? I'll be happy to provide more info, if needed, but I fear this post might already be too long...

7
  • Thanks, Eric, I'll consider it next time :) Is there a way to move it, else ?
    – Kzwix
    Commented Jun 20, 2016 at 13:28
  • 1
    "root can be most anybody" looks like your admins have it backward.
    – romainl
    Commented Jun 20, 2016 at 13:28
  • 2
    romainl: no, it's a test server, and, as such, we more or less all have root access on it for testing purposes. However, the homes of our unprivileged accounts are mounted on ALL servers, and, as such, should not be compromised by root access on this server :)
    – Kzwix
    Commented Jun 20, 2016 at 13:30
  • 1
    See :help initialization. You'll find environment variables there that you can set that will help you accomplish this.
    – Randy Morris
    Commented Jun 20, 2016 at 13:31
  • Randy: Thanks, but I've already had a look there (that's how I knew to set the VIM environment variable - which doesn't seem to produce an effect, by the way), and, well, I'm asking here because I didn't manage to find an answer in that help section :) Is there someting more specific you'd suggest ?
    – Kzwix
    Commented Jun 20, 2016 at 13:38

1 Answer 1

4

A simple/hacky workaround for this is to use -u to point to a vimrc that uses so (abbreviated version of source) to read the vimrcs you want to load in the order in which you want to load them. For example,

" this is our vimrc specified by -u
so /etc/vimrc
so /tmp/personal_vimrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.