How to reference/source a custom .vimrc file

后端 未结 9 1268
生来不讨喜
生来不讨喜 2020-12-08 04:24

Is there a way to reference (or \"source\") another user\'s .vimrc file?

When I kuu (a variant of su that uses kerberos security tokens) to

相关标签:
9条回答
  • 2020-12-08 04:57

    I've created a plugin years ago since I wanted my own .vimrc on our servers without forcing any user who uses sudo to navigate as root to my .vimrc.

    My plugin works exactly as the plugin what @ULick provided.

    GIST

    0 讨论(0)
  • 2020-12-08 04:58

    I'm assuming that your initial owner owns your tty. If so, you can get your initial USER with:

    stat -c'%U' `tty`
    

    By placing your customized root .vimrc in /root/.vimrc.$USRNAME you can keep a reasonably secure customized vimrc file. You can do other things too, but I leave that to your imagination.

    Method 1 - put this in your /root/.bashrc & smoke it:

    # Source a custome vimrc if it exists
    mytty=`tty`
    initial_user=`stat -c'%U' $mytty`
    custom_vimrc="/root/.vimrc.$initial_user"
    if [ -f $custom_vimrc ]; then
        export VIMINIT="source $custom_vimrc"
    fi
    

    Method 2 - put something similar in your /root/.vimrc (a better solution since you might use ksh).

    If anyone can figure out Method 2, I'd welcome the post. I lack motivation.

    0 讨论(0)
  • 2020-12-08 05:01

    You can use the MYVIMRC environment variable. This way, you won't have to pass -u each time you fire up vim. (You can of course do an alias instead, but that won't help with e.g., vipw)

    Keep in mind that .vimrc can execute arbitrary commands, if you use /home/user/.vimrc you may be creating a security issue (e.g., someone manages to compromise your user account, changes your .vimrc, and then gets root the next time you edit a file as root). You can, of course, keep a known-safe copy in ~root/ somewhere.

    You could assumably even set something up in ~root/.bashrc to automatically set MYVIMRC to something different for each different administrator.

    0 讨论(0)
  • 2020-12-08 05:03

    I've only ever attempted this a few times and this seems to work fine for me. Define an alias for vim that is something like the following:

    alias vim="HOME=~yournormaluser vim -c 'let \$HOME = \"$HOME\"'"
    

    What this does is trick vim into using your $HOME/.vim/ environment, yet resets $HOME from within vim so doing things like :e ~/something.txt will still use the admin user's $HOME.

    This has the added advantage that you don't have to change the admin's ~/.vimrc at all.

    0 讨论(0)
  • 2020-12-08 05:10

    Method 2 - as an addition to .vimrc

    tried different things
    tty does not work, and system("who am i") neither (they come up empty when used from within vim-function), so this way is much longer. Any shortcuts are welcome

    "Local .vimrc for the user
    " 1. get the user, which used su
    " 2. we can load his .vimrc.<user>
    " from $HOME (from where we have sudo'ed in)
    let b:term = substitute( system ("ps T | grep ' ps T$' | sed -e 's/^  *//' | cut -d ' ' -f 2 "), "\n", "", "" )
    let b:user = substitute( system ("who | grep ".b:term." | cut -d ' ' -f 1 "), "\n", "", "" )
    let b:file = $HOME."/.vimrc.".b:user
    if filereadable(b:file)
      execute 'source '.b:file
    endif
    
    0 讨论(0)
  • 2020-12-08 05:17

    In vim:

    :source /path/to/your/.vimrc

    0 讨论(0)
提交回复
热议问题