Can I use perl regular expressions in the vim command line?

10,205

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.

Share:
10,205

Related videos on Youtube

user51549
Author by

user51549

Updated on September 17, 2022

Comments

  • user51549
    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
      dreftymac about 4 years
      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
      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
    Mark K Cowan about 9 years
    +1, :help perl-patterns has resolved the one thing that I previously hated about vim
  • alextercete
    alextercete almost 7 years
    This is the answer I was looking for!
  • Nico
    Nico over 5 years
    also, 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
    mattmc3 about 4 years
    You can also help yourself out by adding nnoremap / /\v and vnoremap / /\v to your .vimrc so that when you type / it just works.
  • drevicko
    drevicko about 3 years
    Ancient answer, but can you explain what \v is doing?
  • Ross Jacobs
    Ross Jacobs about 3 years
    @drevicko Take a look at this documentation: vimdoc.sourceforge.net/htmldoc/pattern.html
  • drevicko
    drevicko about 3 years
    Ah, 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