Wednesday, May 9, 2012

Resist Read Only Realities, Part I

Part I - The Beast Bespoke

One thing I frequently find limiting in Vim is the disturbingly all
too frequent read-only commands that display data in a pager-like dump
with the only options being: space, d, j, b, u, k and q for moving
down and up and quitting the pager. This is lame. Sometimes we want
to be able to interact with that information, or see a filtered subset
of it, or have at it with even more freedom and flexibility to do with
as we choose.

Take for example, the :scriptnames command that shows all of the
scripts Vim has loaded since the session started, in load order. We
most commonly use this list to check for the presence of a new plugin
we've installed, to confirm that it's loading correctly. Indeed, this
is one of the diagnostics #vim denizens will demand of you when
presenting with plugin failures.

If all you have is a small handful of scripts, then perusing the
output of :scriptnames manually in a dumb pager is tolerable, if not
enjoyable. If you have a lot of plugins then this quickly becomes
painful. Wouldn't it be nice to be able to filter your :scriptnames
list so that it only showed scripts matching the pattern you're
looking for? What we crave is something like:

:scriptnames /\cnerd/

to select only the loaded scripts matching the case-insensitive
pattern 'nerd' (presumably to catch one of either NERDCommenter or
NERDTree.)

That would be nice. That is what we desire. But we are left wanting.
In fact, we're left with the unsavoury message:

E488: Trailing characters

Are we doomed to a baleful life of blind bumbling through screenfuls
of awkwardly navigated, 1970's style non-interactive data dumps?

No. We don't have to suffer this injustice. We are vimmers. We have
recourse. We can solve this problem with Vim's :redir command, in
various ways, such as:
  • :redir > somefile
  • :redir => somevar
  • :redir @{a-zA-Z*+"}>   (to a register)
The Standard Redir Recipe

Solving the :scriptnames problem above with the file redirection solution:

:redir >myscripts
:silent scriptnames
:redir END
:edit myscripts
:v/\cnerd/d


This works, and isn't too painful to do manually when you need it,
so long as you don't need it too often. The :scriptnames command is a
prime example of one you wouldn't need to call very often. Having to
choreograph the five line dance above once in a blue moon is no great
ordeal, provided you can easily remember the right steps in the right
order.

Is this as good as it gets?

Well, actually, no. This is just the first step, showing you the crux
of the problem and the simplest of Vim's built-in solutions. Coming up
next, I will show you how to take it to the next level with a splash
of split, filter, join love.

No comments:

Post a Comment