|
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
|