Wikia

Vim Tips Wiki

Changes: Script for choosing vim background color

Edit

Back to page

(Comments: Problem is still not clear.)
(Insert TipProposed template + minor manual clean)
Line 1: Line 1:
While choosing colors in gvim is very easy, done by a single ":colorscheme some-name" command, in contrast it's much harder for vim users to do the same. Here is a small script which may help choosing a background color for vim users.
+
{{TipProposed
  +
|id=0
  +
|previous=0
  +
|next=0
  +
|created=August 8, 2012
  +
|complexity=basic
  +
|author=
  +
|version=7.0
  +
|subpage=/201208
  +
|category1=
  +
|category2=
  +
}}
  +
While choosing colors in gvim is very easy, done by a single <code>:colorscheme some-name</code> command, in contrast it's much harder for Vim users to do the same. Here is a small script which may help choosing a background color for Vim users.
   
 
==script: vim_bg_color_test.vim==
 
==script: vim_bg_color_test.vim==
 
 
<pre>
 
<pre>
 
" script name : vim_bg_color_test.vim
 
" script name : vim_bg_color_test.vim
 
"
 
"
" dedicated to choose a background color in vim (not gvim), for ":hi Normal".
+
" dedicated to choose a background color in Vim (not gvim), for ":hi Normal".
" Needed here is an integer ranging from 0 to 255. (vs rgb values for gvim)
+
" Needed here is an integer ranging from 0 to 255. (vs rgb values for gvim)
 
" (Does color value effect depends on operating used ?)
 
" (Does color value effect depends on operating used ?)
" (Here foreground color is set to black but script may help on choosing
+
" (Here foreground color is set to black but script may help on choosing
 
" foreground color too.)
 
" foreground color too.)
 
"
 
"
" Execute this script from within vim by ":source %" if you edit it currently.
+
" Execute this script from within Vim by ":source %" if you edit it currently.
 
" Beginning and ending values are asked for in order to do a loop on colors.
 
" Beginning and ending values are asked for in order to do a loop on colors.
 
" The status line at the bottom of screen shows script effect.
 
" The status line at the bottom of screen shows script effect.
 
" Type a carriage-return after seeing color showed (and associated number).
 
" Type a carriage-return after seeing color showed (and associated number).
 
"
 
"
" In principle the lot of highlight (:hi) options are well chosen by vim
+
" In principle the lot of highlight (:hi) options are well chosen by Vim
" when ":hi Normal" options are set, while possibly adding in .vimrc (linux) :
+
" when ":hi Normal" options are set, while possibly adding in .vimrc (linux) :
 
" :set background=dark or light
 
" :set background=dark or light
 
" :set t_Co=256
 
" :set t_Co=256
Line 25: Line 24:
 
let n=input("First number (0 to 255)")
 
let n=input("First number (0 to 255)")
 
let n2=input("Last number (0 to 255)")
 
let n2=input("Last number (0 to 255)")
while n <= n2
+
while n <= n2
exe "hi Normal ctermfg=black ctermbg=".n
+
execute "hi Normal ctermfg=black ctermbg=".n
exe input("Value ".n )
+
execute input("Value ".n )
 
let n=n+1
 
let n=n+1
 
endwhile
 
endwhile
Line 42: Line 41:
 
set t_Co=256
 
set t_Co=256
 
"set background=dark " dark/light if useful
 
"set background=dark " dark/light if useful
" The ctermbg option has been chosen using vim_bg_color_test.vim script
+
" The ctermbg option has been chosen using vim_bg_color_test.vim script
 
highlight Normal ctermfg=black ctermbg=222
 
highlight Normal ctermfg=black ctermbg=222
 
endif
 
endif
 
</pre>
 
</pre>
   
==References==
+
Vim documentation gives only one example, for Vim users:
Vim documentation gives only one example, for vim users:
+
<pre>
:highlight Normal ctermfg=grey ctermbg=darkblue
+
:highlight Normal ctermfg=grey ctermbg=darkblue
  +
</pre>
  +
  +
and Vim gives access to "colortest.vim" script which seems to me too difficult to tweak.
   
and vim gives access to "colortest.vim" script which seems to me too difficult to tweak.
 
 
==Comments==
 
==Comments==
 
I don't understand what problem you're solving here. What's wrong with colorschemes in a color terminal? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 17:45, August 8, 2012 (UTC)
 
I don't understand what problem you're solving here. What's wrong with colorschemes in a color terminal? --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 17:45, August 8, 2012 (UTC)
   
:The problem is not with gvim (colorscheme is easy to use), as said, but with vim.
+
:The problem is not with gvim (colorscheme is easy to use), as said, but with Vim.
:Coming back to the script. It is working well inside vim 7.3 on ubuntu. I am not sure it's ok everywhere, since selected background color may sometimes not be visible, on the bottom line of the screen. I don't know why. --Alain b, 17:46, October 9, 2012
+
:Coming back to the script. It is working well inside Vim 7.3 on ubuntu. I am not sure it's ok everywhere, since selected background color may sometimes not be visible, on the bottom line of the screen. I don't know why. --Alain b, 17:46, October 9, 2012
 
::Please spell out what the problem is (readers should not need to wonder about the purpose of a tip). Identify a system where there is a problem, then state what the problem is. Then mention how the tip overcomes the problem. Perhaps this is intended for some cut-down version of console Vim where color schemes don't work? I normally use gvim, but I'm pretty sure I have used color schemes on Windows and Linux in terminals (although I built them myself, so perhaps I had some feature enabled?). To sign a comment, add four tildes (<code><nowiki>~~~~</nowiki></code>) at the end of the last line. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 10:54, October 14, 2012 (UTC)
 
::Please spell out what the problem is (readers should not need to wonder about the purpose of a tip). Identify a system where there is a problem, then state what the problem is. Then mention how the tip overcomes the problem. Perhaps this is intended for some cut-down version of console Vim where color schemes don't work? I normally use gvim, but I'm pretty sure I have used color schemes on Windows and Linux in terminals (although I built them myself, so perhaps I had some feature enabled?). To sign a comment, add four tildes (<code><nowiki>~~~~</nowiki></code>) at the end of the last line. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 10:54, October 14, 2012 (UTC)

Revision as of 07:59, January 12, 2013

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created August 8, 2012 · complexity basic · version 7.0

While choosing colors in gvim is very easy, done by a single :colorscheme some-name command, in contrast it's much harder for Vim users to do the same. Here is a small script which may help choosing a background color for Vim users.

script: vim_bg_color_test.vim

" script name : vim_bg_color_test.vim
"
" dedicated to choose a background color in Vim (not gvim), for ":hi Normal".
" Needed here is an integer ranging from 0 to 255. (vs rgb values for gvim)
" (Does color value effect depends on operating used ?)
" (Here foreground color is set to black but script may help on choosing
" foreground color too.)
"
" Execute this script from within Vim by ":source %" if you edit it currently.
" Beginning and ending values are asked for in order to do a loop on colors.
" The status line at the bottom of screen shows script effect.
" Type a carriage-return after seeing color showed (and associated number).
"
" In principle the lot of highlight (:hi) options are well chosen by Vim
" when ":hi Normal" options are set, while possibly adding in .vimrc (linux) :
" :set background=dark or light
" :set t_Co=256
" 08/08/2012 - alain_b
"----------------------------------------------
let n=input("First number (0 to 255)")
let n2=input("Last number (0 to 255)")
while n <= n2
  execute "hi Normal ctermfg=black ctermbg=".n
  execute input("Value ".n )
  let n=n+1
endwhile
" Initial display restored
source ~/.vimrc

Your vimrc should contain something like this:

if has("gui_running")
  colorscheme peachpuff
else
  set t_Co=256
  "set background=dark  " dark/light if useful
  " The ctermbg option has been chosen using vim_bg_color_test.vim script
  highlight Normal ctermfg=black ctermbg=222
endif

Vim documentation gives only one example, for Vim users:

:highlight Normal ctermfg=grey ctermbg=darkblue

and Vim gives access to "colortest.vim" script which seems to me too difficult to tweak.

Comments

I don't understand what problem you're solving here. What's wrong with colorschemes in a color terminal? --Fritzophrenic (talk) 17:45, August 8, 2012 (UTC)

The problem is not with gvim (colorscheme is easy to use), as said, but with Vim.
Coming back to the script. It is working well inside Vim 7.3 on ubuntu. I am not sure it's ok everywhere, since selected background color may sometimes not be visible, on the bottom line of the screen. I don't know why. --Alain b, 17:46, October 9, 2012
Please spell out what the problem is (readers should not need to wonder about the purpose of a tip). Identify a system where there is a problem, then state what the problem is. Then mention how the tip overcomes the problem. Perhaps this is intended for some cut-down version of console Vim where color schemes don't work? I normally use gvim, but I'm pretty sure I have used color schemes on Windows and Linux in terminals (although I built them myself, so perhaps I had some feature enabled?). To sign a comment, add four tildes (~~~~) at the end of the last line. JohnBeckett (talk) 10:54, October 14, 2012 (UTC)

Around Wikia's network

Random Wiki