Wednesday, June 27, 2007

dip and vip it.

Thats right, paragraph manipulation is as easy as three keys. I tend to just use 'dip' and 'vip', but there have gotta be other good ones out there...

yip? yep.

grip? Rip? darn! No, that doesn't work. Changes you back into insert mode.

cip? okay, yes.

g?ip? Yes, kinda silly though.

>ip? yes.

'ip' is just inner paragraph. You could do various kinds of inner blocks and quotes.

Wednesday, May 23, 2007

Convenient tab commands

I was conversing with a friend about how to turn a split into a new tab, and vice versa this morning. You can call IMing 'conversing', right? Yes. At this day and age, I'd hope so. Anyway. We came up with a couple convenient commands/abbreviations. Take your pick:

" Close a split and then open it in a new tab:
:ca tb let a=bufnr("%") q tabnew exe "b ". a
:command! Tb :let a=bufnr("%") | q | tabnew | exe "b ". a

" Close the current tab and open as a split in another existing tab:
:command! Ts :let a=bufnr("%") | tabprev | sp | exe "b ". a | tabnext | q | tabprev
:ca ts :let a=bufnr("%") tabprev sp exe "b ". a tabnext q tabprev

Note these obviously aren't that smart. If you have no splits, its not gonna act quite right. I've made the order open X, then close Y so that vim still has *something* open. Hopefully that means your Vim session won't disappear out from underneath you.

Yep, I just tested it out. Vim doesn't close, and the 'tb' command works fine regardless of the # of windows open. The 'ts' command acts a little funny when there is more than one split open in a tab. It removes one split, sticks it in another tab but doesn't completely remove the other tab b/c there's still a window in it. Not so bad. Just a little wierd.


Monday, May 21, 2007

Recursive Diffs?

It appears that, yes, someone has already gone out and written a plugin for recursive diffing two folders. According to one person, it works relatively well. What I'm wondering is, could this plugin be used to diff two revisions in say, SVN?

Well, I had a chance to try the thing out and I have to say that I am happier doing:

:r ! diff -r --brief

and then making my own quick macro to diff any files mentioned in the diff results. I just like the ability to filter out any lines I already know aren't a factor in my diffing. The DirDiff plugin doesn't allow you to change the contents of the 'diff results'. Well, I was at first. Until I figured out all I had to do was type:

:set modifiable

Then I could delete the 'results' screen willy nilly. Much better. It still complained if I tried to insert text or what not...so its kind of a toss up. If I needed to quickly copy the contents back and forth then this DirDiff tool would be just the thing.

The trouble is most of the time I'm actually looking for a needle in the haystack. Some little change in a text file is causing a world of havoc for me. I don't want to filter out 'types' of files, and copy whole swathes of differences between the trees. In this case, I think manually reading in the diff and recording your own little quick diff macro is the way to go. DirDiff is slightly too constrained in what it wants to let you do.

In summary: a good plugin if you need to copy back and forth changes, or you don't want to spend the extra 3 minutes it takes to record your own diffing macro. I've rated this script 'Helpful'.

Tuesday, May 15, 2007

Quickly delete several random lines

I just couldn't think of a quick way to take a subset of a file, where some # of lines had been randomly deleted. Since VIM doesn't have its own random() command you have to turn to some external method of determining which lines to delete. My solution was ruby:

:ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")

To repeat the command several times I couldn't think of anything better than a macro recording:

:let @l=':ruby VIM.command(String(rand(VIM::Buffer.current.count-1)+1) +"|d")^M'

Where '^M' is really Control-J cariage return. Then to do it...uh, 30,000 times I just typed 30000@l

Saturday, February 17, 2007

Indent level highlighting

I have finally begun to delve into the world of VIM syntax highlighting. The syntax used by VIM that give all my different files such pretty colors have always been something of a mystery to me. Recently a problem caused me to make the jump into the :help syntax pages.

There was on my part some assumption that someone had already done what I wanted to do, which is to highlight each successive indent level in a file. This would be particularly helpful for python files, but other programming languages as well. The BlockHL looked like just what I was looking for. Interesting, and helpful, but not quite.

Delving into the documentation I eventually found after much scratching of the head how to do exactly what I wanted to. Piddling around with the highlight colors I came up with three variations. I think I like the right most one best.





















BlockHL syntax pluginProgressively darker indenting, kinda loud.


Alternate coloring. Better.Alternate color, with progressively darker shading. Best.

I've posted the very simple coloring script on vim, as a script plugin. See its page for further details.

*** update ***

Well well well, apparently some poeple actually read my postings sometimes and call me out on my shoddy programming skillz. Yes, its true. This plugin only works with TABs. Maybe if I get less lazy, I'll make it work with the tabstop/shiftwidth settings for you non tab users.

Tuesday, February 06, 2007

switching between bash and batch scripts.

Were it only as easy as s/sh/tch/! Well, it almost is.

For the most part only a few search replaces are required. Most of the tme I need to convert a dos batch to a bash script. The first round of conversion includes:

:set ft=unix
:%s/%\(\w\+\)%/$\1/g
:%s/set //ig
:%s/rem/#/ig
:%s/;/:/g
:%s/\\/\//g

There is always additional work to do, but these replaces do most of the tedious portion for me.

Thursday, January 11, 2007

Multi Line searches

I often forget about the "newer" searching features in VIM's regular expression quiver: line aware regular expressions. Today, I needed to detect when certain requests had occured twice in a row without any response in between.

First, I grepped the patterns important:

grep "REQ\|RSP" file.txt | vim -

Then I searched for two REQ's in a row:

/REQ.*\_..*REQ

Tada! Wow, that was just so easy.

I haven't played with these kinds of regexps enough to know if you can extend it to say three lines in a row or say two lines with the pattern, and one without, etc. But, I expect it is quite possible with a little work.

Monday, January 08, 2007

Freezing titles ala spreadsheet land.


Every now and then I have to read the results of a SQL query on the command line, and I find the following setup to work wonders in VIM. At least this works well with MySQL, as its command line query tool is kind enough to spit out the output in tab-delineated form.

Say you have a query to perform. Do the following:

echo "select * from tableX;" | mysql -uUser -pPassword databaseName | vim +'set scb sbo+=hor sbo-=ver nowrap ts=30 bt=nofile| sp | exe "norm \<c-w>j\<c-w>_\<c-e>"' -

This will automatically space out the tabs so you can see each of the search result columns with the data, and then it creates two panes - one for the column descriptions, and one for the data. The scroll options are setup so that you can navigate around the data and always see the column description with the data (particularly helpful for large result sets).

The only real improvement I can think of is to use the VIM dbext plugin. I often find myself on several different machines I don't use often enough to warrant customizing plugins/settings, so this quick and dirty sql result browser setting is very handy to have around.

Friday, January 05, 2007

Automatic formatting.

For me it is rare that I use VIM to compose text messages. This is unfortunately due to the nature of writing in VIM: it isn't embedded in the browser window where I am currently composing this blog entry, and it isn't in my mail composer (unless I'm using mutt). Its just not the de facto standard for editing no matter how much I sometimes which is was.

Nevertheless, sometimes I do want to compose a message in VIM. When I do, its usually so that I can take advantage of the automatic formatting features in VIM. Here are the typical settings I use to setup a text editing session:

:set tw=80
:set fo=at

This gets me just the basics: when I insert/delete text in VIM, the text is automatically wrapped/unwrapped.

But I really want my numbered lists to be formatted correctly. In that case I add on an extra flag:

:set tw=80
:set ai
:set fo=atn

Of course, in VIM 7 you can add spell check to that:

:set spelllang=en_us
:set spell

Use the the z= command to find alternate spellings underneath your cursor.