Wikia

Vim Tips Wiki

Perl compatible regular expressions

Talk0
1,610pages on
this wiki
Revision as of 05:28, July 13, 2012 by JohnBot (Talk | contribs)

Tip 393 Printable Monobook Previous Next

created 2002 · complexity intermediate · version 6.0


1. Verify with :ver that +perl or +perl/dyn is compiled in.

2. Install Perl if necessary. On Windows, ActivePerl is standard but any dependency-free perl58.dll will work if you don't need any other perl modules. If you don't want a full install of perl, copy perl58.dll from Strawberry Perl 5.8.x into the folder vim.exe lives and the commands below will work.

3. Type :perldo s/searchme/replaceme/g

Note: +perl/dyn doesn't seem to be necessary.

Comments

Or if you have ruby compiled in (look for +ruby in :ver output), you can use the following:

  • Equivalent to s/pattern/replacement/g
:rubydo gsub /pattern/,'replacement'
  • Equivalent to s/pattern/replacement/
:rubydo sub! /pattern/,'replacement'

The advantage of this tip is that when you know Perl regex well it's easier to write Perl regex than vi regex. Either is fine for simple expressions, but when the expressions get more complex its much easier to work with the syntax you know the best.


Perl regexes also have a different set of "special characters".

For example, the parentheses () are special characters that automatically do grouping and capturing in perl. In a vi regex, they need to be escaped (\( \)) before they'll turn special.


Keep in mind that :perldo is not always enough. Just try to replace something with a newline (:perldo s/<text>/\n/<ENTER>). I get ^@ character instead of a line break. Yeah, I asked for it, but it's not what I wanted.

In this particular case, just use the old "perl pie":

:%!perl -pi -e 's/<text>/\n/'

VimTip6


Script to search using PCRE in Vim: http://groups.yahoo.com/group/vim/message/49561


I know this is a small thing, but you might check out "\v", aka very-magic as a way to make the regular expressions less annoying and more perl-like. It would be nice if I could put set very-magic in my vimrc.


You can also define new command like this:

function s:Substitute(sstring, line1, line2)
  execute a:line1.",".a:line2."!perl -pi -e 'use encoding \"utf8\"; s'".
        \escape(shellescape(a:sstring), '%!').
        \" 2>/dev/null"
endfunction
command -range=% -nargs=+ S call s:Substitute(<q-args>, <line1>, <line2>)
" Example usage:
S(<regex>)[<replacement>]<flags>

"<q-args>" produces an escaped and enclosed in double quotes string, problems with unicode are solved (I hope so) by "use encoding" statement.


eregex.vim

There is a plugin eregex.vim doing the notation translate, from the Vim regex notation to perl/ruby stylish regex notation. However the document is written in Japanese. So I translate some content to explain how to use it.

Install

Vimball file. Open it with Vim and execute :so % .

Quick start

Add the following code to .vimrc file.

nnoremap / :M/
nnoremap ,/ /

Now you can use / to find. :%S// (uppercase S) to replace.

Original Version of eregex.vim is produced by a Japanese.


Around Wikia's network

Random Wiki