summaryrefslogtreecommitdiff
path: root/src/Cool_Vim_Trickery.adoc
blob: cc5e293719e57538ff3147b410463675db7eef5a (plain)
    1 Today I was toying with something absolutely not work related and I wanted to
    2 share it with somebody to show how awesome the vim text editor can be.
    3 
    4 First though, I really would like to thank Bram Moolenaar for contributing such
    5 a useful tool to the world. It may not be the easiest thing in the world to
    6 learn, but once you've got even the most basic functionality figured out, you
    7 can do so much more than other editors will allow. That all goes without even
    8 saying how cool its interface is.  If you realy like vim, you should head on
    9 over to http://www.vim.org/index.php[his website] and buy a
   10 http://www.freewear.org/?org=Vim[tshirt],
   11 http://www.vim.org/stickers.php[sticker], or a
   12 http://www.vim.org/posters.php[poster].
   13 
   14 What I was dabbling with was vim colors for syntax highlighting. It turns out
   15 that the stock install of vim for Arch linux comes with almost 200 color
   16 schemes. I really wanted to see them all but didn't want to have to keep typing
   17 ":colors schemename". That is a lot of repeat key presses after all, something
   18 we Linux folk really are not fond of when faced with a situation that a
   19 computer can handle for us (automation - what a novel idea).
   20 
   21 After some searching, I discovered
   22 http://vim.wikia.com/wiki/Switch_color_schemes#Switching_colors[this vim
   23 script] that will change your color scheme forwards or backwards by pressing F8
   24 or Shift+F8, respectively. Really neat, but not super automated still. Who
   25 wants to set this sucker to a timer and watch it switch every 200 milliseconds?
   26 I do I do I do!
   27 
   28 That vim script provides a few functions that are bound to the afforementioned
   29 hotkeys. The function we are immediately concerned with is called
   30 _NextColor_. This will switch the color scheme to the next in the list.
   31 
   32 Here's where vim gets really cool, even though it already is.
   33 
   34 It turns out that there is a list in this vim script that is a statically coded
   35 array of scheme names, so if you have more themes installed than those listed
   36 in the array, you're out of luck unless you manually add them. Now, at this
   37 point we could probably have vim run a shell command and massage the output to
   38 make an array for us at runtime, but where's the fun in that (that's just a
   39 little TOO automated for the purposes of this article)? I want to rock some vim
   40 find and replace regex!
   41 
   42 
   43 [[inserting-shell-command-output]]
   44 == Inserting Shell Command Output
   45 
   46 So now, the first thing we're going to do is insert the output of a shell
   47 command to our vim file, specificall +ls -1+. When in command mode, run
   48 
   49 ----
   50 :read !ls -1 /usr/share/vim/vim73/colors/
   51 ----
   52 
   53 This should insert a metric bitt load (teehee) of lines if you have very
   54 many color schemes.
   55 
   56 
   57 [[ranges-in-regex]]
   58 == Ranges in Regex
   59 
   60 From here, we want to massage the data with a few vim find and replace regexes.
   61 Establish the line that your file list ends at. For me, this was line 207, but
   62 this very likely won't be the case for you. Move the cursor to the first line
   63 and run the following in command mode
   64 
   65 ----
   66 :.,207s/\.vim//
   67 ----
   68 
   69 This will do a find and replace on the text range starting where the cursor is
   70 currently (the .) and ending at line 207 (the 207). After that it's just a
   71 standard regex substitution. This should chop off the '.vim' at the end of each
   72 filename.
   73 
   74 Next, we need to remove the new lines, comma delimit, and encase in single
   75 quotes to match the array format. Again, place your cursor at the first line of
   76 your list. Remember the line number of the last line in the list?
   77 
   78 ----
   79 :.,207s/\(.*\).vim\n/'\1', /
   80 ----
   81 
   82 In this cryptic regex, we replace from the current line (the .) to line 207 any
   83 line containing .vim with a line break after it (the .vim\n) with the text
   84 preceeding the .vim ( captured by +<\(.*\)+), encasing that value with single
   85 quotes and ending with a comma space (the +'\1',+) encase the entire string
   86 with a [ ] and you'll be set. Just erase the old array set your new one to
   87 +s.mycolors+ near the top.
   88 
   89 
   90 [[setting-the-rotate-timer]]
   91 == Setting the Rotate Timer
   92 
   93 Now there's one piece left: set the timer. In command mode, do the following
   94 and hit enter
   95 
   96 ----
   97 :while 1 | sleep 1000m | call NextColor(1) | endwhile
   98 ----
   99 
  100 That will rotate through every theme you just added to your array every 1000
  101 milliseconds. Just change the +1000m+ to whatever you want to make it update at
  102 different intervals.
  103 
  104 Hello worthless but super awesome functionality!
  105 
  106 ----
  107 :while 1 | sleep 1000m | call NextColor(0) | endwhile
  108 ----
  109 
  110 
  111 Category:Linux
  112 Category:Vim
  113 
  114 
  115 // vim: set syntax=asciidoc:

Generated by cgit