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 You can map the combination \v
individually to all the regex you set up./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
- Install VIM on MacOS the right way
- Do you like regex? Meet the Regular Expressions community on Telegram
- Want to learn regex? Go to Aurélio Jargas Marinho ‘s website, a great reference on regex in Brazil. There’s lots of good stuff there!
- An excellent regex tester is Regex Expressions 101