Vim Tips Wiki
Line 104: Line 104:
 
----
 
----
 
--[[User:Wraithlord|Wraithlord]] 13:58, 6 November 2008 (UTC)Your modification works great. It also served to show me some elements of VIM scripting. I thank you for both.
 
--[[User:Wraithlord|Wraithlord]] 13:58, 6 November 2008 (UTC)Your modification works great. It also served to show me some elements of VIM scripting. I thank you for both.
  +
  +
----
  +
I've tried to add some more colors to the colstr, but the functionality I got was broken.
  +
I've added a suffix to said string as follows:
  +
<pre> let colstr .= ' novula midnight revolutions camo lilac moria' </pre>
  +
  +
Result is strange cycling behavior.
  +
  +
--[[User:Wraithlord|Wraithlord]] 14:15, 11 November 2008 (UTC)

Revision as of 14:15, 11 November 2008

Tip 955 Printable Monobook Previous Next

created June 28, 2005 · complexity basic · author Georg Dahn · version 6.0


Pan Shizhu's color scheme ps_color script#760 supports two styles, a cool (dark) and a warm (light) style. If you want to switch between these styles by just a single key stroke, you may define the following function in your vimrc:

function! s:SwitchPSCStyle()
  if exists('g:psc_style')
    if g:psc_style == 'cool'
      let g:psc_style = 'warm'
    elseif g:psc_style == 'warm'
      let g:psc_style = 'cool'
    endif
  else
    let g:psc_style = 'warm'
  endif
  colorscheme ps_color
endfunction
map <silent> <F6> :call <SID>SwitchPSCStyle()<CR>

Set colorscheme from a list

 TO DO 

  • Decide what to do with above SwitchPSCStyle function.
  • Probably move following to Rotate color themes.

The following script allows you to easily cycle forwards and backwards through a list of your favorite color schemes (change the colstr variable in the script to the schemes you want). Press F8 to set the next scheme, or Shift-F8 to set the previous.

To experiment, select the script in Vim, then press y to copy, then :@" to execute the script. If you want the script to always be available, save it as file ~/.vim/plugin/setcolor.vim (Unix) or $HOME/vimfiles/plugin/setcolor.vim (Windows), then restart Vim.

" Set next or previous colorscheme from a list.
function! SetNextColor(forward)
  if exists('g:colors_name')
    let colstr = 'default elflord peachpuff desert256 breeze morning'
    let colstr .= ' darkblue gothic aqua earth black_angus relaxedgreen'
    let colstr .= ' darkblack freya motus impact less chocolateliquor'
    let missing = []
    let colors = split(colstr)
    let current = index(colors, g:colors_name)
    for i in range(len(colors))
      let current += (a:forward ? 1 : -1)
      if !(0 <= current && current < len(colors))
        let current = (a:forward ? 0 : len(colors)-1)
      endif
      try
        execute 'colorscheme '.colors[current]
        break
      catch /E185:/
        call add(missing, colors[current])
      endtry
    endfor
    redraw
    if len(missing) > 0
      echo 'Error: colorscheme not found:' join(missing)
    endif
    echo g:colors_name
  endif
endfunction
nnoremap <F8> :call SetNextColor(1)<CR>
nnoremap <S-F8> :call SetNextColor(0)<CR>

Comments

I thought it'd be nice to just use a list of favorite colors with a small loop for making the change in color. Something along the lines of the following:

if exists("g:colors_name")
  let colors_list = ["default", "peachpuff", "desert256", "breeze", "morning", "darkblue", "gothic", "aqua", "earth", "black_angus", "relaxedgreen", "darkblack", "freya", "motus", "impact", "less", "chocolateliquor" ]
  let l:found_color = 0
  for my_color in colors_list
    echo my_color
    if my_color == g:colors_name
      let l:found_color = 1
    endif
    if l:found_color == 1
      exec "colorscheme ".my_color
      let l:found_color = 0
    endif
  endfor
  if l:foundcolor == 0
    exec "colorscheme ".colors_list[1]
  endif

I'm far from knowing well the VIM language and no surprise the construct doesn't really work. It fails actually when executing "colorscheme my_color". I guess to someone who knows the language fixing this is trivial.

Wraithlord 13:44, 29 October 2008 (UTC)


I've modified the above to use the ":exec" command. I have not tested it at all, but it stands a better chance of working. --Fritzophrenic 21:24, 29 October 2008 (UTC)

I'm currently cleaning up some of the highlight tips, and had intended getting around to selecting a colorscheme, so I've done a complete overhaul of the script and put it in this tip. I'll probably move it elsewhere in due course (and delete these comments), but I'll leave this here for a week so everyone has a chance to see the reply. Thanks Wraithlord for starting me. --JohnBeckett 23:42, 29 October 2008 (UTC)

--Wraithlord 13:58, 6 November 2008 (UTC)Your modification works great. It also served to show me some elements of VIM scripting. I thank you for both.


I've tried to add some more colors to the colstr, but the functionality I got was broken. I've added a suffix to said string as follows:

 let colstr .= ' novula midnight revolutions camo lilac moria' 

Result is strange cycling behavior.

--Wraithlord 14:15, 11 November 2008 (UTC)