Monday, July 26, 2010

Overview of Regex in Vim's Command Interface

I've been asked about substitutions in Vim many times, so I figured this would make for a useful post. This is just a quick overview of this feature of Vim, but it should handle most of your needs. If you're interested in hearing more, email me and I'll add another post. It's also probably a bit helpful to be familiar with regular expressions when you read this.

First, I'll talk about the form of the regex. It looks like this:

restriction command flags

Think about it this way, and you'll be okay. Here's an example to help you wrap your head around the idea. This example substitutes all occurrences 'goodbye' for 'hello' in the first three lines:

1,3 s/hello/goodbye/g

The restriction in this case is 1,3 which signifies a range. The command is s/hello/goodbye/. The flag is g. Hopefully, you can clearly see the three distinct parts of the command. Let's talk about these a bit more in-depth. The first part I'm going to talk about is the command, because it's the most prominent of the three.

The Command

The command can be either a search:

/pattern/

or a substitution:

s/pattern/string/

Using a slash-delimited(/) regex will do a forward search/substitution. If you want to search backwards, you can delimit your regex with question marks(?) like so:

s?pattern?string?

The rest behaves essentially like a standard regular expression. Next, I'll talk about flags.

The Flags

There are only four flags and they go immediately after the final delimiter of the command. The four flags are g, c, i, and I. Here are their meanings:

g - global - continue searching after the first occurrence on the line; most searches/subs stop after they find a match
c - check - prompt to check if you want to continue with substitution; works only in substitutions
i - case-insensitive - ignore case when searching
I - case-sensitive - check case while searching; this is the default behavior

The Restriction

The restriction is used to specify which lines are affected by the search/substitution. It can specify one or many lines and can be specified by a line number or a regex that will return the number of the first line it finds a match in.

To specify one line, simply write the line number or the regex and then a space, like so:

3 s/day/night/

This will replace the first occurrence of 'day' with 'night' in the third line. To use a regex, do this:

/day/ s/day/night/

This regex finds the first line with 'day' in it and replaces the first occurence of 'day' with 'night'. To do a range of lines, place a line specifier (a line number or regex) before and after a comma like so:

10,/night/ s/day/night/

This takes all lines (inclusively) between the tenth one and the first occurrence of 'night' after that, and replaces the first occurrence of 'day' with 'night' in each line.

Now I know this might seem like a lot, but give some practice and it will make perfect sense in due time. Try to use this whenever possible to get extra practice in.

Hope this was helpful! Comment or email me directly for any questions, comments, or suggestions; I'd be more than happy to help! Don't forget to mark your reaction to this article below!

No comments:

Post a Comment