For those who may be interested, I’ve created a repo on GitHub where I plan to dump small functions and scripts I write while helping people on irc or on Stack Overflow. In these scripts I will try my hardest to use “best practices” so they can be used as examples when writing your own scripts.
Please let me know if you notice anything you would do differently. The point of this is to serve as digestable examples of proper vimscript.
Check it out at vimscript-snippets.
Published on November 07, 2011 Comments
· · ·
Recently I was searching for a fuzzy-finding plugin to make working with projects a little easier. I posted a quick hack that I still use every now and then, however I’ve recently discovered a rather new plugin called CtrlP.
For the uninitiated, fuzzy-finding plugins allow you to open a file by only typing enough pieces of the path to uniquely identify a file. Sometimes this is as little as one or two characters. I wasn’t really sold on this behavior until I tried using them at work where we have many files with similar names. A plugin like this makes dealing with such situations much easier to deal with than tab-completion alone.
CtrlP is written entirely in vimscript. This is a big plus for me because many of the machines at work aren’t built with +python or +ruby which some of the other fuzzy-finding plugins require. Although it’s written in vimscript it caches directory and filenames for larger directories and can be configured to always use a cache. This means it’s quite slow on first launch in larger directories but is very fast on subsequent calls.
Another big plus is the configurability CtrlP offers. There are (currently) 22 options provided to configure everything from mappings to how caching should work. You can even delegate the file indexing out to a faster external mechanism if you so choose.
Couple all of this with an active and responsive developer that provides excellent documentation and you’ve got my current favorite script.
Published on September 12, 2011 Comments
· · ·
I’ve never liked the fact that vim doesn’t allow users to define
commands that start with a lowercase letter. Every time I create one I
end up having to also make a cnoreabbrev that lets me use my lowercase
version. This has the ugly side effect of having text transform as you
type. It also will make the cursor jump around if you’re doing more
than changing the case of things. For example:
cnoreabbrev ack ack<c-\>esubstitute(getcmdline(), '^ack\>', 'Ack!', '')<enter>
This allows me to type “:ack ” and have it automatically change to
“:Ack! ” as soon as I press <space>. The text changes automatically
and my cursor jumps to the right two spaces instead of just one.
Obviously this is a small inconvenience but it is an annoying one for
me.
I have come up with a bit of a hacky workaround for this that allows more transparency and I thought I’d share in case someone out there is as stubborn as me. The following function will run a substitution on the current command line:
function! CommandLineSubstitute() let cl = getcmdline() if exists('g:command_line_substitutes') for [k, v] in g:command_line_substitutes if match(cl, k) == 0 let cl = substitute(cl, k, v, "") break endif endfor endif return cl endfunction
This function reads search/replace pairs from a global variable you set
in your .vimrc. That global variable looks something like this:
" note that line continuation is only possible without 'C' in 'cpoptions' let g:command_line_substitutes = [ \ ['^ack ', 'Ack! '], \ ['^ee \(.\+\)', 'e **/\1*'], \ ['^h ', 'vertical help '], \]
The last thing we need to do is a single map to call this function
whenever we press <enter> on the command line:
cnoremap <enter> <c-\>eCommandLineSubstitute()<enter><enter>
While this configuration is a bit ugly, it saves me the inconvenience of having a bunch of abbreviations lying around and makes the substitution transparent to me.
I’ve only been using this for a few hours but I haven’t hit any issues thus far. If you try this out and have problems or if you know of a way to simplify it please let me know in the comments.
Published on August 03, 2011 Comments
· · ·
I recently realized I have been using a very simple replacement for the
Command-T/Fuzzy Finder type plugins for quite some time,
however I had never made it convenient for myself to do so. Today I
pushed a small change to my .vimrc that I think will make things a
little easier for me.
nnoremap <leader>ff :e **/*<left> nnoremap <leader>fp :<c-p><left>
The first map allows me to quickly search for a file (by partial name)
anywhere under the current directory using <leader>ff. If there are
multiple files found that match the glob, vim will throw E77. If that
happens I can use <leader>fp to load the previous fuzzy-find on the
command line with my cursor where it needs to be for me to make the
search more specific.
Obviously this solution is nowhere near as complete as a plugin dedicated to this task, however it’s as complete as I need it to be.
Published on August 01, 2011 Comments
· · ·
Lately I’ve been doing more programming at home on the iMac and I’ve been having some very noticeable wrist pains as a result of using that tiny keyboard. Since I use a fully split keyboard at work I figured I’d go ahead and find another one I could use at home. After a bit of research I ended up grabbing another Kinesis off eBay. I would say it’s a Kinesis classic, however there is no branding on it whatsoever. My guess is that it was their original model.
Almost everyone who has seen it thus far thinks I’m crazy, here are some pictures so you can see why:
I must say I’m quite happy with it so far. I’m having a lot of trouble with the curly-braces/square-brackets and the backspace keys. I’ve always hit the spacebar with my left thumb so now I’m gettinloosentencethaloolikthis. Once that muscle memory is gone I think I’ll be fine. I already feel almost up to speed when typing normal text but programming is significantly more difficult at this point.
Overall I think this was a good purchase. Maybe later this year I’ll buy one of the more modern (and much more expensive) models.
Oh, and for those who may be interested, here’s the label:
Published on July 28, 2011 Comments