Loading ...
Sorry, an error occurred while loading the content.

60068Re: color scheme switcher

Expand Messages
  • Tim Chase
    Jul 6, 2005
    • 0 Attachment
      >>nnoremap <f4> :exec "colorscheme
      >>".substitute(substitute(expand($VIMRUNTIME."/colors/*.vim"),
      >>'.*[/\\]'.colors_name.'\.vim\c[[:cntrl:]]*[^[:cntrl:]]*[/\\]\([^.]*\).*',
      >>'\1', 'g'), '[^[:cntrl:]]*[/\\]\([^.]*\).vim\c.*','\1','')<cr><cr>
      >
      > Wow -- that's one crazy line, but it works great!

      Glad it solves what ails ya :)

      In case you want to tweak or understand it, I'll try and break it
      down into less cryptic pieces.

      The root of it is a mapping which executes "colorscheme <stuff>"

      The <stuff> is found by getting a list of the files; finding your
      current colorscheme in that list; then finding the next one in
      the list to pass back to the previous bit.

      To get the list of files, the mapping uses the expand() function.
      This is a simple version that simply hits one directory. Multiple
      directories might be doable with a little tweaking by appending
      multiple calls to the expand() function together, and placing
      some delimiter (char 0x0A seems to be what expand() uses).

      To get the *next* name, we look for the current name:

      ...[/\\]'.colors_name.'\.vim\c'

      which should take into account path separators (either forward or
      back-slash), followed by the current color-scheme name, followed
      by ".vim" (case-insensitive "\c").

      Because the expand() call separates the lines with a control
      character (0x0A, IIUC) we then slurp up those control characters,
      and then all non-control-characters up to a forward/back-slash.
      We then tag the stuff that follows, up to a period. We then slurp
      up and discard the (case-insensitive) ".vim" and anything that
      follows. We now have the next color-scheme name tagged, so we
      replace the whole lot with the tagged stuff.

      This is good. However, we then encounter the problem raised
      earlier--that when you're on the last colorscheme, the whole
      substitute() fails to find the "next" one because it's at the end
      of the list, so it doesn't change anything. Thus, you get back
      the original results of the expand() call. This chokes the
      "colorscheme <stuff>" statement. So we simply strip off the path,
      the ".vim" and anything that follows, leaving just the first
      colorscheme in the list.

      The first <cr> then runs the whole lot, and the second <cr>
      dismisses the "Press <enter> to continue" message.

      ...and Bob's your uncle :)

      -tim
    • Show all 13 messages in this topic