How to enable Extended REGEX in VIM

Escaping characters that should have special meanings in regex can be very annoying. But VIM has an interesting feature called very magic! Here's how to use it.

Estimated reading time: 5 minutes

Introduction

In general, there are far less insane ways of creating regular expressions than within VIM. That’s why you might want to use other tools to test, validate and then import your regex into the text editor.

Save time with a good tester

An interesting regex tester is regex101.com, which helps a lot when composing a huge expression. There are other very nice ones, but this is my favorite.

Once you’ve come up with the perfect expression, you discover in VIM that you now have to escape characters that shouldn’t be literal! And then you have to turn them into specials to make it all work…

But there is a great solution, which consists of calling an internal option called very magic, which will help a lot to avoid escaping lots of ( { } ) in long regular expressions.

Very Magic!

So, to use very magic, just insert an additional command before the regular expression: \v

In a simple and uncreative example 😆, we match group 1 (Magia) and then bring it back with \1, adding very magic, ignoring anything else that is not matched in this expression.

#sem very magic
:%s/\v\(Magia\) furta-cor/\1 very magic/g

#com very magic
:%s/\v(Magia) furta-cor/\1 very magic/g

So far, so good. It doesn’t seem to get in the way too much. But let’s take a longer example, where not using very magic can be a real problem.

We have 6 fields in a list of products and we need to make some substitutions and insertions of lines and data to fit into a .txt file for generating NFe from Sefaz.

1|9388552610010|Item A|44.0000|10.0404545455|441.78
2|9488542618041|Item B|3.0000|14.0980000000|70.49
3|9588532619058|Item C|41.0000|4.9614285714|69.46
4|9583522613157|Item D|9.0000|4.5585714286|223.37
5|9588122623188|Item E|18.0000|4.8527777778|87.35
6|9384562634201|Item F|14.0000|28.2492857143|395.49
7|9285562614225|Item G|17.0000|10.7370588235|182.53
8|9186562647232|Item H|8.0000|3.2850000000|26.28
9|9280562617256|Item I|36.0000|125.0611111111|4502.20
10|9188552617270|Item J|14.0000|15.4914285714|216.88
11|9178552627324|Item K|17.0000|5.2288235294|88.89

The list was inserted into the regex101.com texter, perfect, all the fields matched.

If you put it directly into VIM, it simply won’t work. Here’s what it should look like, without using \v very magic:

#sem very magic, lotado de escapes

:%s~^\(\d+\)\|\(\d\{13\}\)\|\(.*\)\|\(\d+\.\d\{4\}\)\|\(\d+\.\d\{10\}\)\|\(\d+\.\d\{2\}\)$~H|\1||\rI|\2||\3|49019900||5102|un|\4\|\5|\6||un|\4|\5|||||1|||||||\rM||\rN|\rN10d|0|103|\rO||||999|\rO07|99|0.00|\rO11|0.0000|0.0000|\rQ|\rQ04|07|\rS|\rS04|07|~g

Now, with \v

#com very magic, mais legível, (mais) compatível com outros robôs de regex, como o do regex101, do sed -E etc

:%s~\v^(\d+)\|(\d{13})\|(.*)\|(\d+\.\d{4})\|(\d+\.\d{10})\|(\d+\.\d{2})$~H|\1||\rI|\2||\3|49019900||5102|un|\4\|\5|\6||un|\4|\5|||||1|||||||\rM||\rN|\rN10d|0|103|\rO||||999|\rO07|99|0.00|\rO11|0.0000|0.0000|\rQ|\rQ04|07|\rS|\rS04|07|~g

Note: group mirrors in various regex engines are usually called with $: $1 $2 $3, but in VIM it is \1 \2 \3

In order not to run into the problems of very magic and just enjoy the good stuff, there is one important thing to know: several characters that have no special meaning in other regex robots, now do here, in VIM.

Using \v, some very strange characters are given special meaning, such as @ % & ~ . So if you need them as literals, escape them with \

@%&~

:%s/\v\@\%\&\~

Automating very magic

Unfortunately it’s not possible to set the very magic option in .vimrc. You’ll have to apply \v individually to all the regex you set up. You can map the combination /s to generate a /s\v automatically by adding the cnoremap in your ~/.vimrc (Thanks to colleague Micael Levi for the tip in Telegram’s excellent Regular Expressions community).

 " Mapeia s/ para /s\v
 cnoremap s/ s/\v

” Map /s to /s\v
cnoremap s/ s/\v

Find out all about very magic and other magic options in VIM’s :help

:help magic

(...) Use of "\v" means that after it, all ASCII characters except '0'-'9', 'a'-'z', 'A'-'Z' and '_' have special meaning: "very magic"

Questions, suggestions? Leave them in the comments!

See also

Comentários

More articles

How to use bc, the shell calculator

bc = bench calculator.
If you don’t know your shell’s calculator yet, it’s time to learn how to use it, even if its use is very basic.
The most trivial use of its functions should already cover most of your needs.
But don’t be fooled, this is a really powerful piece of software that should definitely be on your radar.

Read the article "

URL encoding/decoding with sed

There are various ways of encoding/decoding urls.
Programmers often use ready-made functions for this.
But do you really know what these functions are doing?
For this article, I’ve chosen sed as the tool to replace the codes and I point out the RFCs that discuss the subject.

Read the article "

Understand how to customize VIM on MacOS

If you’re already starting to get the hang of VIM, it’s time to take the next steps.
In this article, I’ll explain how to set up VIM for MacOS in what I consider to be the cleanest way (you may want to install it differently and that’s fine) and how to configure the NerdTree plugins, to access the directory tree; Status Tab to put some additional tools on the screen (and make VIM look very nice); and the Git plugin, to make version control easier without leaving the application.
Happy reading!

Read the article "

How to display colors in the terminal

Do you want to display texts with colors, bold, italics, underline, etc.?
Understanding a few rules and codes makes it easier than it sounds.
Learn how to display colors in your terminal with the clarity of someone who knows what they’re doing.

Read the article "
bureau-it.com