DEV Community

Igor Irianto
Igor Irianto

Posted on • Updated on

Learning Vim Regex

Learning Vim Regex

Learning regular expressions is like learning latin. It is not pleasant but it is good for your soul. They are incredibly useful but also hard to read. People who master them are hailed as gods. Many desired to learn them but a few actually did.

To a programmer, regex (regular expression) is a tool. It can be learned (and mastered). Learning it is a lifetime investment. Here's why: First, regex is portable. Sure, there are different regex flavors, but the basic principle is the same across all flavors. Regex knowledge in one domain will transfer everywhere. Second, regex is powerful and knowing it can save you a lot of time. Programming is all about creating and updating text. Regex allows you to search and modify texts efficiently. Many powerful programs (like parsers) are built on top of regexes. Third, it is fun (once you get to a certain proficiency).

Regex knowledge boosts your Linux-fu. You'll unlock more powerful commands using programs like sed, awk, grep, find, vim, and more. In this article, we will focus on learning and understanding regexes in Vim.

This article is by no means a complete regex tutorial. In fact, I'll be honest right now, that this article probably contains less than a third of what Vim regex can do. But they are the ones that I find very useful. I call them the good parts.

Many of the regex information you will be learning here is also transferable. Although the syntax may vary slightly in different environments, the principle is the same. If you understand everything in this article, you should be well on your way to use regex in other environments.

Using Regex in Vim

Many of Vim's search features allow you to use regular expressions. Some of them are:

  • The search commands (/ and ?)
  • The global and substitute command-line (ex) commands (:g and :s)
  • The grep commands (:vimgrep and :grep)

There are other places where you can use regex, but based on my experience, these three are the main places I would use regex in.

Finally, this is not a guide about how to use the global command, or grep, or whatever. This is more of a regex guide than a Vim guide. Think of it as a regex guide that uses Vim. It will show you how to use regex in Vim.

I am going to limit the command to only the search command. Almost everything you see here is done using the search command / (I may release more guides in the future with different commands).

To Magic or Not

If you are acquainted with Vim regex, you might be aware of Vim's special "flavor" of Regex. You can read more in :h /magic.

Basically Vim allows you to pass an option where certain characters are treated as literal characters while others as special characters.

In this guide, I won't use any magic option - meaning no \v, \m, \V, or \M (you basically will see a lot of backslashes).

Enough with introductory stuff, let's start!

The Good Parts of Vim Regex

Basic Search /

The most basic search that you could do is to search for a literal word. If you want to search for the string donut, run /donut. Vim will look for a literal word donut. It won't match d0nut, Donut, or DONUT (unless you have 'smartsearch' option on, but that's another topic).

In real life, things are not that simple. Maybe you need to look for variations of foo string including food, fool, and foos. Maybe you need to match a phone-number-looking pattern like xxx-xxx-xxxx where x could be any integer. So you want this pattern to match 123-123-1234 and 333-444-5555, but not 1234-123-123.

Practically speaking, we often need to search for a pattern, not a literal word. This is where knowing a little bit of regex goes a long way. In fact, just by knowing a handful of regex concepts in this article, you should be able to handle 99% of your searching needs.

Any Character .

The most simple, versatile, and almost universal regex pattern is the dot symbol (.). It matches any single character.

For example, if you need to search for a 3-letter string that starts with an n and ends with a t, and you don't care what goes in the middle, you can use the pattern n.t.

/n.t will match the strings not, nut, net, n0t, and even n t (space between n and t).

You can also use it multiple times, so /n..b matches any 4 character string that starts with n, followed by any character, followed by any character, and ends with b. So it will match noob, n00b, n3wt. It will not match nob or nooob.

The Character Class

The dot syntax is useful, but sometimes you want to classify your search. Recall that /n.t matches any three-lettered word that starts with n, ends with t, and anything goes in the middle. What if we only want alphabetic characters, so it would match not, nut, and net but not