In this post we will look at some simple examples of using regular expressions in vim to do more advanced searching. This based on some updates I needed to make to my reading list yaml file. We will look at doing pattern matching with the following yaml file:
- author: Author1
title: Title1
- author: Author2.
title: Title2: a Title
- author: Author3.
title: Title3
- author: Author4
title: Title4: a Title
- author: Author5
title: Title5[inside brackets]
We will look using regular expression pattern matching to help us with the following tasks:
- Remove unnecessary periods from the end author lines.
- Put titles that have a colon inside quotes.
- Find titles that include brackets.
The search feature of Vim is capable of working with regular expressions, although with a special syntax. Below we will look at how to search for each task.
Recall that the search feature is done by pressing /
.
Finding Authors with Periods
To find the authors that end in a period we match in the following order:
- First match
author:
. - Next match any number of characters that aren’t a newline
\n
or a period\.
. - Finally match a period.
So, we search the following regular expression (the /
is used to start the search):
/author:[^\n\.]*\.
This pattern matches author: Author2.
and author: Author3.
, which is what we want.
Finding the Titles with a Colon
This is similar to above, but now we match using colons instead of periods. So we search:
/title:[^\n:]*:
This pattern matches title: Title2:
and title: Title4:
.
Finding the Title with Brackets
This search is a little tricky. It is possible to do it incorrectly and still get a postive result. A
correct regular expression to use is
/title:[^\n\[]*\[
Notice how we used \[
instead of [
to search for the brackets.
Now, the incorrect expression /title:[^\n[]*[
will still match, but it is sort of by chance that this
works. If we extend the search expression, then we get a break in the match. For example, the
pattern
/title:[^\n\[]*\[inside brackets\]
will correctly match, but the expression
/title:[^\n[]*[inside brackets]
will NOT match.