Perl compatible regular expressions
From Vim Tips Wiki
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.
+perl/dyn doesn't seem to be necessary.
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.
- Maybe it works to let Perl parse the \n instead of Vim? I don't have Perl support in my Vim, but maybe using \\n instead of \n might work. --Fritzophrenic (talk) 15:35, August 7, 2013 (UTC)
In this particular case, just use the old "perl pie":
:%!perl -pi -e 's/<text>/\n/'
For whatever reason (in my vim), I need to use "double quotes" instead of "single quotes" like this for substituting (hope this helps someone):
:%!perl -pi -e "s/<text>/\n/"
- This command relies on your shell to run Perl. So if your shell doesn't support single quotes it won't work. Windows in particular does NOT support single quotes in its default shell (cmd.exe). --Fritzophrenic (talk) 15:35, August 7, 2013 (UTC)
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.
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.
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.