5

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

  • 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 '15 at 21:36
  • maybe as a sudo its not loading the right vimrc? Try "source <pathtoyourvimrc>". – philolo1 Aug 9 '15 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 '15 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 '15 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 '15 at 12:56
8

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
| improve this answer | |
  • 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 '15 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 '15 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 '15 at 17:09
3

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'.

| improve this answer | |
3

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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