7

I installed colorscheme "jellybeans" for Vim. Why does the colors look differently when I run Vim with and without sudo?

Example on the screen below - the left one is without sudo, the right is with sudo. The one with sudo is working properly - how can I make Vim to have the same colors without using sudo?

enter image description here

I use

Linux Mint 17.2 (x64)  
gnome-terminal 3.6.2  
bash 4.3.11  
tmux 1.8  
Vim 7.4

The value of my $TERM variable is:

$TERM=screen-256color

And the result is the same with sudo $SHELL -c 'echo $TERM'

Here is my vimrc file.

EDIT - SOLUTION:

Colorscheme file was lacking read permission for "others". Fixed with:
sudo chmod +r /usr/share/vim/vim74/colors/jellybeans.vim

6
  • 1
    sudo echo $TERM will always print the same value as echo $TERM - the variable is expanded before sudo comes into play. What does sudo $SHELL -c 'echo $TERM' say? How did you install the colour scene? And you should use sudoedit instead of sudo vim.
    – muru
    Aug 8, 2015 at 21:36
  • maybe as a sudo its not loading the right vimrc? Try "source <pathtoyourvimrc>".
    – philolo1
    Aug 9, 2015 at 6:54
  • @muru sudo $SHELL -c 'echo $TERM' gives me the same value of "screen-256color". I installed the colorscheme by copying jellybeans.vim file into the folder /usr/share/vim/vim74/colors. Thank you for sudoedit tip.
    – Kossak
    Aug 9, 2015 at 9:30
  • 1
    @philolo1 i think sudo vim loads my local .vimrc because i deleted root's .vimrc and all my plugins/keys work as the should. I tried source /home/kossak/.vimrc but it doesn't change anything.
    – Kossak
    Aug 9, 2015 at 9:53
  • Please don't add solutions to your answer. Mark your answer as accepted. There might be a timeout, but you can accept your own answers.
    – muru
    Aug 10, 2015 at 12:56

3 Answers 3

10

When you run vim with sudo, you are running as user root, so you are using root's vimrc and .vim. Your colorscheme and other customizations set in your ~/.vimrc are not being used. You can see this by executing

:scriptnames
3
  • I don't hink only root .vimrc file is used when running sudo vim. Just now i deleted root's .vimrc file and my keys/plugins defined in my user's .vimrc still work when running sudo vim. (when i log as root first and then run vim, they don't work). When i run scriptnames i see jeelybeans.vim only with sudo vim not with vim.
    – Kossak
    Aug 9, 2015 at 9:48
  • this :scriptnames led me to solution - jellybeans.vim file was lacking read permission for "others". Modify your answer so i can mark it as solution. Thank you :)
    – Kossak
    Aug 9, 2015 at 9:56
  • Thank you, but I don't think my answer was the solution. In fact, after reading your solution and re-reading your question, I see that I misunderstood your question.
    – garyjohn
    Aug 9, 2015 at 17:09
4

Instead of running sudo vim to run vim as root, run sudoedit filename.

This creates a temporary copy of the file, edits that copy without root privileges, and copies the changes to the original file. You may need to set VISUAL or EDITOR to vim (e.g. export VISUAL=vim) to get sudoedit to use the correct editor. This is safer and uses your Vim configuration instead of root’s.

If you want to use gvim instead, you’ll need to use the --nofork option: export VISUAL='gvim --nofork'.

1
  • Thanks. Never heard of sudoedit before. Mar 16 at 12:58
4

The solution was: making sure users who run vim can actually read the colorscheme file. Giving everyone read permission is done with:

sudo chmod +r /usr/share/vim/vim74/colors/jellybeans.vim

Your Answer

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

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