28
6
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
28
6
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
20
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.
1also, 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! – Ayrat – 2018-03-27T08:20:57.583
10
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
2+1, :help perl-patterns
has resolved the one thing that I previously hated about vim – Mark K Cowan – 2014-08-26T16:57:13.433
8
You can also use:
/\v"your regex"
instead of:
/"your regex"
This is the answer I was looking for! – alextercete – 2016-12-06T14:28:40.673
1You can also help yourself out by adding nnoremap / /\v
and vnoremap / /\v
to your .vimrc so that when you type /
it just works. – mattmc3 – 2019-08-16T20:29:12.117
3
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.)
2
Use the eregex.vim plugin. It's very useful and I have had no problems with it.
From :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 – 2019-09-07T14:23:16.007