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.