{"id":4870,"date":"2021-07-15T22:25:35","date_gmt":"2021-07-16T01:25:35","guid":{"rendered":"https:\/\/bureau-it.com\/artigos\/how-to-enable-extended-regex-in-vim\/"},"modified":"2024-09-19T22:33:48","modified_gmt":"2024-09-20T01:33:48","slug":"how-to-enable-extended-regex-in-vim","status":"publish","type":"post","link":"https:\/\/bureau-it.com\/en\/artigos\/how-to-enable-extended-regex-in-vim\/","title":{"rendered":"How to enable Extended REGEX in VIM"},"content":{"rendered":"\n
<\/span>Estimated reading time: <\/span>5<\/span> minutes<\/span><\/p>\n\n In general, there are far less insane ways of creating regular expressions than within VIM<\/a>.\nThat’s why you might want to use other tools to test, validate and then import your regex into the text editor. <\/p>\n\n An interesting regex tester is regex101.com<\/a>, which helps a lot when composing a huge expression.\nThere are other very nice ones, but this is my favorite. <\/p>\n\n 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!\nAnd then you have to turn them into specials to make it all work… <\/p>\n\n But there is a great solution, which consists of calling an internal option called very magic<\/em>, which will help a lot to avoid escaping lots of So, to use very magic, just insert an additional command before the regular expression: In a simple and uncreative example \ud83d\ude06, we match group 1 So far, so good.\nIt doesn’t seem to get in the way too much.\nBut let’s take a longer example, where not using very magic can be a real problem. <\/p>\n\n 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.<\/p>\n\n The list was inserted into the regex101.com texter<\/a>, perfect, all the fields matched.<\/p>\n\n If you put it directly into VIM, it simply won’t work.\nHere’s what it should look like, without using Now, with Note: group mirrors in various regex engines are usually called with $: 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.<\/p>\n\n Using ” Map \/s to \/s\\v Find out all about very magic<\/em> and other magic<\/em> options in VIM’s :help<\/p>\n\n Questions, suggestions?\nLeave them in the comments! <\/p>\n\n Escaping characters that should have special meanings in regex can be very annoying.Introduction<\/h2>\n\n
Save time with a good tester<\/h2>\n\n
( { } ) <\/code>in long regular expressions. <\/p>\n\n
Very Magic!<\/h2>\n\n
\\v<\/code><\/p>\n\n
(Magia) <\/code>and then bring it back with
\\1<\/code>, adding
very magic<\/code>, ignoring anything else that is not matched in this expression.<\/p>\n\n
#sem very magic\n:%s\/\\v\\(Magia\\) furta-cor\/\\1 very magic\/g\n\n#com very magic\n:%s\/\\v(Magia) furta-cor\/\\1 very magic\/g<\/pre>\n\n
<\/a><\/figure><\/li>
<\/a><\/figure><\/li><\/ul><\/figure>\n\n
1|9388552610010|Item A|44.0000|10.0404545455|441.78\n2|9488542618041|Item B|3.0000|14.0980000000|70.49\n3|9588532619058|Item C|41.0000|4.9614285714|69.46\n4|9583522613157|Item D|9.0000|4.5585714286|223.37\n5|9588122623188|Item E|18.0000|4.8527777778|87.35\n6|9384562634201|Item F|14.0000|28.2492857143|395.49\n7|9285562614225|Item G|17.0000|10.7370588235|182.53\n8|9186562647232|Item H|8.0000|3.2850000000|26.28\n9|9280562617256|Item I|36.0000|125.0611111111|4502.20\n10|9188552617270|Item J|14.0000|15.4914285714|216.88\n11|9178552627324|Item K|17.0000|5.2288235294|88.89<\/pre>\n\n
<\/figure>\n\n
\\v<\/code> very magic: <\/p>\n\n
#sem very magic, lotado de escapes\n\n:%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<\/pre>\n\n
\\v<\/code><\/p>\n\n
#com very magic, mais leg\u00edvel, (mais) compat\u00edvel com outros rob\u00f4s de regex, como o do regex101, do sed -E etc\n\n:%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<\/pre>\n\n
<\/a><\/figure><\/li>
<\/a><\/figure><\/li>
<\/a><\/figure><\/li><\/ul><\/figure>\n\n
$1 $2 $3<\/code>, but in VIM it is
\\1 \\2 \\3<\/code><\/p>\n\n
\\v<\/code>, some very strange characters are given special meaning, such as
@<\/code>
%<\/code>
&<\/code>
~<\/code> .\nSo if you need them as literals, escape them with
\\<\/code><\/p>\n\n
@%&~\n\n:%s\/\\v\\@\\%\\&\\~<\/pre>\n\n
<\/figure>\n\n
Automating very magic<\/h2>\n\n
Unfortunately it’s not possible to set the very magic option in .vimrc. You’ll have to apply \\v<\/code> individually to all the regex you set up.<\/s> You can map the combination
\/s<\/code> to generate a
\/s\\v<\/code> automatically by adding the
cnoremap<\/code> in your
~\/.vimrc<\/code> (Thanks to colleague Micael Levi for the tip in Telegram’s<\/a> excellent Regular Expressions community<\/a>).<\/p>\n\n
\" Mapeia s\/ para \/s\\v\n cnoremap s\/ s\/\\v<\/pre>\n\n
cnoremap s\/ s\/\\v<\/p>\n\n:help magic\n\n(...) Use of \"\\v\" means that after it, all ASCII characters except '0'-'9', 'a'-'z', 'A'-'Z' and '_' have special meaning: \"very magic\"<\/pre>\n\n
See also<\/h2>\n\n
\nBut VIM has an interesting feature called very magic<\/i>! Here’s how to use it. <\/p>\n","protected":false},"author":2,"featured_media":4684,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"resumo_insta":"","imagem_insta":"","acessibilidade_insta":"","hashtags_insta":"","resumo_linkedin":"","imagem_linkedin":"","hashtag_linkedin":"","resumo_face":"","imagem_face":"","hashtag_face":"","footnotes":""},"categories":[86,87],"tags":[89,90,88],"class_list":["post-4870","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-regex-en","category-vim-en","tag-extended-regex-en","tag-regular-expressions","tag-vim-en"],"yoast_head":"\n