How VIM helps to learn regex

Many Linux users prefer to use simpler editors like Nano, because they found VIM confusing, unnecessary complex and too hard to use. Here I can explain you, how VIM helped me to better understand tools like sed, grep, rename, awk, less, etc. which use similar rules and logic.

For those who had never used VIM, it is usually hard to get into basics of the *nix tools mentioned above which support regex (regular expressions) for different manipulations with text:

  • Search
  • Replace
  • Delete
  • Filter

Regex rules and patters in the beginning look very confusing and counterintuitive. And, honestly saying, they are so. But when you use VIM as your editor, many basic regex rules is easier to remember.

Example 1

VIM motion command to go to the end of line is $ (just press key $ in the NORMAL mode of VIM). Let’s search for all strings which end with t in VIM:

/t$

Now the same in sed and grep commands in the shell:

1
2
3
4
5
# First with sed
sed -n '/t$/p' input_file

# And now with grep
grep 't$' input_file

Both commands produce the same result.

Example 2

To go to the first non-empty character in the line VIM you shall press ^ key.

Now I want to search for all strings in the file which start from letter T in VIM:

/^T

This command will highlight all strings beginning with T.

Now let’s find all strings starting with T in a given file with sed and with grep commands:

1
2
3
4
5
# First with sed
sed -n '/^T/p' input_file

# And now with grep
grep '^T' input_file

Search and replace

Example 3

Let’s find all bad and replace with good. To make this in VIM, I will use following command in COMMAND mode:

%s/bad/good/g

g tells VIM to replace all found bad, otherwise it will replace only first match.

Now the same in sed.

1
sed 's/bad/good/g' input_file

As you can see, syntax is absolutely the same.

Example 4

VIM and sed have very nice feature, which simplifies search syntax and allows user to use different than slash symbol to separate sections in the substitute command.

E.g. if I want to replace / (slash symbol) with the word “slash” in a file. If I use slash as a delimiter in the substitute command in VIM and in sed, then I have to use backslash before slash in the matching pattern section. VIM command:

%s/\//slash/g 

And sed command:

1
sed 's/\//slash/g' input_file

I suppose you can agree that this code is a bit hard to read. But I can write this command differently. For VIM:

%s$/$slash$g

And for sed:

1
sed 's$/$slash$g' input_file

It is important to remember, that it works only in substitute in sed. But VIM for both global and substitue commands you can use different symbol as delimiter.

E.g. in VIM the following:

%s/\//slash/g
%g/\//d

and

%s#/#slash#g
%g#/#d

are the same.

Instead of # you can use any other symbol, e.g. $ or |. Usually I use symbol which is not used in the match string or regex pattern.

Search and delete

Example 5

Let’s remove all empty lines from our file. In VIM command it can be done with this command:

%g/^$/d

g means that command is global (executed on every match in the opened file).

In order to match empty lines, we just put ^ (beginning of the line) and $ (end of the line) nearby each other. And after pattern I use command d, which means delete.

Similar in sed and grep:

1
2
3
4
5
# First in sed
sed '/^$/d' input_file

# Then is grep
grep -v '^$' input_file

As we can see, sed syntax looks very similar to syntax in VIM.

Example 6

Here I want to delete all lines except ones which start with the letter V.

VIM command for this action:

:v/^V/d

And equivalent command in sed and grep:

1
2
3
4
5
# In sed negation is done with "!"
sed '/^V/!d' input_file

# In grep it is also "v" like in VIM.
grep -v '^V' input_file

Delete

Example 7

To delete lines from 6 to 8 in some file the following commands are used.

In VIM:

:6,8d

In sed command looks very similar.

1
sed '6,8d' input_file

Logical “or” (multiple matches)

Often it is needed to match multiple different patterns for substitute, delete or any other manipulation.

Example 8

E.g. I want to find all word easy and hard in the file.

In VIM:

/easy\|hard

In sed and grep:

1
2
3
sed -n '/easy\|hard/p' input_file

grep "easy\|hard" input_file

Example 9

Same syntax is for the substitution. Let’s replace all words easy and hard with doable in some file.

In VIM:

:%s/easy\|hard/doable/g

In sed:

1
sed 's/easy\|hard/doable/g' input_file

Knowledge of VIM is not enough

While VIM knowledge can help you with with simple regex syntax, it does not mean that when you learn VIM you can easily write regex rules of any complexity and for any Linux tool. Regex is whole world of itself and person has to learn it anyway.

  • There are many flavors of Regex (POSIX, Perl, Golang, etc.)
  • Each flavor has its own nuances.

In this short post I just wanted to show, that knowledge of VIM helps with introduction into regex and allows you write your own simple rules without much effort.