FASTTRACK
Course Calendar
Book a Course
About Front Runner
Contact Us

Support | Software | Classroom Rentals | Books | File Resources

The VIM editor - by David Slonosky

As an information technology professional, usually you need to work with a lot of text. I mean pure text, like the kind developers or system analysts give you. Maybe the odd database query dump. The occasional log file. A mess, right?

What if you need to extract text from it? Process the file to get some data you need out for your documentation? Clean it up to insert it into your nice system administration guide?.

There is Notepad. Good old Notepad. Free with the purchase of every Windows operating system. Lets you copy, paste, and search. If you like Notepad and think it meets all your needs, then you can stop reading this and go to a new article.

For the rest of you, I would like to introduce you to Vim. Vim is a powerful little text editor that runs on multiple platforms, including Windows and Mac OS X. It does a lot of things I don't pretend to understand, but one of them is the enhanced search and replace using regular expressions. And that is enough for me to have it around on every system I work with.

Regular expressions allow you to do very flexible search and replace patterns. I first got involved with these when I was building WebWorks Publisher templates. See, you got these premade templates, and then management said they didn't fit the corporate requirements, so you learned regular expressions.

Let's use a relatively simple example. You have a set of text with varying amounts of spaces between regularly repeating blocks of text. You want to put in some tab characters between the regularly repeating blocks of text so you can pull it into a pretty Excel spreadsheet.

First of all, what are you searching for? One or more space characters. Or a " " (without the quotes). And what are you trying to replace these with? A tab character, which in the Vim flavour of the regular expression language is "\t" (similar to the ^t you use if you are familiar with Word wildcards).

The search command in Vim is basically [range to search in]s/[what to search for]/[what to replace]/. So let's build the search. We want to look over the whole file, so that's the '%' symbol.

"%s/ /\t/" is the start. But we've already said there are more than one space between our text chunks, and we don't know in advance how many spaces there are going to be, just that there will be more than one space character. So we need to add a qualifier to the search. In this case, we know there will be 2 or more space characters. The Vim regular expression language uses the following to qualify the previous character (really, what is the previous atom, but here our atom is our space character): \{2,\}.

"%s/ \{2,\}/\t/" is nearly it. But Vim is built so that it doesn't do global search and replacements. Just like Word, you have to tell it you want to do a search and replace across the whole file. In Vim, you use 'g' to indicate a 'g'lobal search option. And the option goes at the end.

So our final search and replace command is
"%s/ \{2,\}/\t/g". And there you are with a file where all the blocks of two or more spaces have been replaced by tabs.

Like any automated cleanup, it is rare you will do everything with one command in any editor. But you can get 90-95% done to save you time. Try out Vim, and if you think it's a bit too complex, have a look at Vim/Cream. Cream makes the learning curve a bit less steep. Then maybe you too will want to have Vim on every system you have!

http://www.vim.org/about.php


Front Runner Training
A Division of Front Runner Publishing Solutions Inc.
21 St. Clair Ave. E, Suite 504
Toronto, ON M4T 1L8

Canada
Contact Us
Phone: 416-515-0155
Toll-Free: 1-877-999-0155
Fax:416-849-0437
View a Map (PDF) | Privacy Policy (PDF)