Can I use perl regular expressions in the vim command line?
Solution 1
You can filter any line or range of lines through an external command in vim, using !. E.g., you can do:
:.!perl -pe "s/(\w+)/\u\1/g"
which will filter the current line through that perl command. ( Here :
to get into command line mode, and the .
which follows mean the current line; you can also specify a line range or %
for the whole file, etc.)
If you want to use vim's built in substitution patterns, the closest you'll come is to use vim's "very magic" option, \v, like so:
:s/\v(\w+)/\u\1/g
see :help pattern
and :help substitute
for more details. I don't think "very magic" is quite identical to perl's patterns, but is very close. Anyway, you can always use perl itself if you're more comfortable with it, as above.
Solution 2
You can also use:
/\v"your regex"
instead of:
/"your regex"
Solution 3
No, you can't use Perl regular expressions in that way. For help in learning the Vim equivalents for Perl regular expression components, see
:help perl-patterns
However, you can use Perl as an external filter as explained by frabjous. You can also execute Perl commands within Vim using the Perl interface, if your Vim was compiled with the +perl
feature. See
:help if_perl.txt
Solution 4
Here's a solution from http://vim.wikia.com/wiki/Perl_compatible_regular_expressions
:perldo s/(\w+)/\u$1/g
(Verify with :ver
that +perl
or +perl/dyn
is compiled in.)
Solution 5
Use the eregex.vim plugin. It's very useful and I have had no problems with it.
Related videos on Youtube
user51549
Updated on September 17, 2022Comments
-
user51549 about 1 year
I want to use perl regular expressions on the vim command line. For example, to capitalize the words on the current line, you could type:
:s/(\w+)/\u$1/g
-
dreftymac about 4 yearsFrom :help perl-patterns ... Vim's regexes are most similar to Perl's, in terms of what you can do. The difference between them is mostly just notation uhhmm ... that's like saying Chinese is similar to Greek, in terms of what you can communicate. The difference is mostly just notation. regex is nothing but notation! The differences are annoying if one notation is less familiar than another. That's why people ask about perl in the first place!
-
dreftymac over 2 years@felwithe to be fair ... Vim's popularity and prominence predates that of perl ... although that knowledge does not really do much to ease the pain :/
-
-
Mark K Cowan about 9 years+1,
:help perl-patterns
has resolved the one thing that I previously hated about vim -
alextercete almost 7 yearsThis is the answer I was looking for!
-
Nico over 5 yearsalso, with very magic, you don't have to memorize that
(
is treated specially while{
is not: "all ASCII characters except '0'-'9', 'a'-'z', 'A'-'Z' and '_' have a special meaning." thanks! -
mattmc3 about 4 yearsYou can also help yourself out by adding
nnoremap / /\v
andvnoremap / /\v
to your .vimrc so that when you type/
it just works. -
drevicko about 3 yearsAncient answer, but can you explain what
\v
is doing? -
Ross Jacobs about 3 years@drevicko Take a look at this documentation: vimdoc.sourceforge.net/htmldoc/pattern.html
-
drevicko about 3 yearsAh, ok.
\v
means "very magic": all regex special characters|(){}.*?^$
(did I miss any?) function as is, and need backslashes to get the literal equivalent, as is the case with perl. You can do most of what perl does with look-arounds etc.., but with different syntax: see vimdoc.sourceforge.net/htmldoc/pattern.html#perl-patterns