colors and vimrc in sudo and su

616 views
Skip to first unread message

oversky

unread,
Nov 1, 2009, 3:27:53 AM11/1/09
to vim_use
When I use $sudo vim file, vim uses ~/.vimrc, but not the color
template in ~/.vim/colors.
When I change to super user by su, vim uses some initial vimrc and
color.
How do I apply the user vimrc, color template and plugin for the above
two situations?
I just start using Ubuntu 9.10, and have not used linux before.

bill lam

unread,
Nov 1, 2009, 3:52:59 AM11/1/09
to vim...@googlegroups.com
On Sun, 01 Nov 2009, oversky wrote:
>
> When I use $sudo vim file, vim uses ~/.vimrc, but not the color
> template in ~/.vim/colors.

Not sure what you meant by colors not used. Could you give details of
the your problem, or did you mean the difference between vim and gvim?

--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3

sc

unread,
Nov 1, 2009, 2:27:11 PM11/1/09
to vim...@googlegroups.com

what worked for me is this: as root i changed directory to
/root -- there i created symbolic links to my ~/.vimrc,
~/.gvimrc, and ~/.vim path, where '~' means the home path of
my normal user

sc

James Michael Fultz

unread,
Nov 1, 2009, 7:08:37 PM11/1/09
to vim...@googlegroups.com
* sc <toot...@swbell.net> [2009-11-01 13:27 -0600]:

> > How do I apply the user vimrc, color template and plugin
> > for the above two situations?
> > I just start using Ubuntu 9.10, and have not used linux
> > before.
>
> what worked for me is this: as root i changed directory to
> /root -- there i created symbolic links to my ~/.vimrc,
> ~/.gvimrc, and ~/.vim path, where '~' means the home path of
> my normal user

I think that a better way is to setup environment:

# ~/.profile, ~/.bashrc, ~/.zshenv, etc.
EDITOR=vim
VISUAL=$EDITOR
export EDITOR VISUAL

and:

$ sudoedit file

pansz

unread,
Nov 1, 2009, 7:49:40 PM11/1/09
to vim...@googlegroups.com
oversky 写道:

for the first case, it should work, ubuntu by default will setup sudo to
use the user environment. but ubuntu by default do *not* have the
proper version of vim installed, you need to :sudo apt-get install vim
before you can use vim.

for the second case, you are using root environment, so you can do under
your user account:
sudo ln -s ~/.vim /root/.vim
sudo ln -s ~/.vimrc /root/.vimrc

then use your su and you can get the same vim env.

bill lam

unread,
Nov 1, 2009, 9:56:41 PM11/1/09
to vim...@googlegroups.com

or

$ sudo -e file

pansz

unread,
Nov 5, 2009, 2:05:50 AM11/5/09
to vim...@googlegroups.com
bill lam 写道:

> On Sun, 01 Nov 2009, James Michael Fultz wrote:
>> * sc <toot...@swbell.net> [2009-11-01 13:27 -0600]:
>>>> How do I apply the user vimrc, color template and plugin
>>>> for the above two situations?
>>>> I just start using Ubuntu 9.10, and have not used linux
>>>> before.
>>> what worked for me is this: as root i changed directory to
>>> /root -- there i created symbolic links to my ~/.vimrc,
>>> ~/.gvimrc, and ~/.vim path, where '~' means the home path of
>>> my normal user
>> I think that a better way is to setup environment:
>>
>> # ~/.profile, ~/.bashrc, ~/.zshenv, etc.
>> EDITOR=vim
>> VISUAL=$EDITOR
>> export EDITOR VISUAL
>>
>> and:
>>
>> $ sudoedit file
>
> or
>
> $ sudo -e file
>

sudoedit type 1 more key than sudo vi

sudo -e needs the same number of keystrokes, but sudo vi is easier to
remember.

So why do you think sudo -e or sudo edit is better than sudo vi ?

bill lam

unread,
Nov 5, 2009, 2:22:00 AM11/5/09
to vim...@googlegroups.com

just a habit. actually sudo edit is easier to type than sudo -e
because the "-" is away from home position, that depends on keyboard
layout of course.

btw iirc nautilus allows mouse right click to call customised script
that bind to 'gsudo gvim file'.

James Michael Fultz

unread,
Nov 5, 2009, 8:54:36 AM11/5/09
to vim...@googlegroups.com
* pansz <pans...@routon.com> [2009-11-05 15:05 +0800]:

The latter does not preserve your personal Vim environment.

See sudo(8):

-e The -e (edit) option indicates that, instead of running a command,
the user wishes to edit one or more files. In lieu of a command,
the string "sudoedit" is used when consulting the sudoers file. If
the user is authorized by sudoers the following steps are taken:

1. Temporary copies are made of the files to be edited with the
owner set to the invoking user.

2. The editor specified by the VISUAL or EDITOR environment vari‐
ables is run to edit the temporary files. If neither VISUAL
nor EDITOR are set, the program listed in the editor sudoers
variable is used.

3. If they have been modified, the temporary files are copied back
to their original location and the temporary versions are
removed.

If the specified file does not exist, it will be created. Note
that unlike most commands run by sudo, the editor is run with the
invoking user’s environment unmodified. If, for some reason, sudo
is unable to update a file with its edited version, the user will
receive a warning and the edited copy will remain in a temporary
file.

pansz

unread,
Nov 8, 2009, 8:17:32 PM11/8/09
to vim...@googlegroups.com

James Michael Fultz 写道:

>>
>> So why do you think sudo -e or sudo edit is better than sudo vi ?
>
> The latter does not preserve your personal Vim environment.
>
oops, got it.

I setup my sudo to always preserve my personal environment for all
commands, so I do never need the sudo -e.

Matt Wozniski

unread,
Nov 10, 2009, 5:38:32 PM11/10/09
to vim...@googlegroups.com

It's also horrifically dangerous to use "sudo vim" when "sudoedit"
would do. "sudo vim" means that vim runs as root, so a malicious
script, or a vim bug, could have catastrophic consequences.
"sudoedit" runs vim as your user, so malicious scripts or catastrophic
bugs can't result in an "rm -rf /" or worse being run.

If you don't absolutely *need* to run vim as root, I'd recommend
against it. And, if you *do* need to run vim as root, I'd recommend
disabling all plugins, colorschemes, syntax highlighting, etc. But
that's just my $0.02 - people less concerned with running an editor
with a history of exploitable bugs as root are of course welcome to do
so.

~Matt

pansz

unread,
Nov 10, 2009, 7:45:45 PM11/10/09
to vim...@googlegroups.com
Matt Wozniski 写道:

> On Sun, Nov 8, 2009 at 8:17 PM, pansz wrote:
>> James Michael Fultz 写道:
>>>> So why do you think sudo -e or sudo edit is better than sudo vi ?
>>>
>>> The latter does not preserve your personal Vim environment.
>>>
>> oops, got it.
>>
>> I setup my sudo to always preserve my personal environment for all
>> commands, so I do never need the sudo -e.
>
> It's also horrifically dangerous to use "sudo vim" when "sudoedit"
> would do. "sudo vim" means that vim runs as root, so a malicious
> script, or a vim bug, could have catastrophic consequences.
> "sudoedit" runs vim as your user, so malicious scripts or catastrophic
> bugs can't result in an "rm -rf /" or worse being run.

It convince me.

So the difference is: sudo -e copies the file in tmp and edit as user,
while sudo vi edit the file as root.

Hope more user know it.

Reply all
Reply to author
Forward
0 new messages