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.

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>/

/<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

instead of Vim? I don't have Perl support in my Vim, but maybe using \

instead of

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>/

/'

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>/

/"

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)

The inplace (i) is a no-op in this case, so just use

:%!perl -pe 's/a/b/'

--~~~~

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.

I found the above did not work for me, and played around with it until I got something working (2018):

A way to call perl directly for substitution [ edit | edit source ]

" Use local perl to handle substitution " Invoke via :S/pattern/replace/flags function s:Substitute(line1, line2, sstring) let l:lines=getline(a:line1, a:line2) " Call perl using utf8. #line etc makes error messages prettier let l:sysresult=system("perl -e 'use utf8;' -e '#line 1 \"perl substitution\"' -pe ". \shellescape("s".escape(a:sstring,"%!").";"), l:lines) if v:shell_error echo l:sysresult return endif let l:result=split(l:sysresult, "

", 1) " 1: don't drop blank lines " delete lines but don't put in register: execute a:line1.",".a:line2." normal \"_dd" call append(a:line1-1, l:result) " add lines call cursor(a:line1, 1) " back to starting place if a:line1 == a:line2 echom "Substitution on line" a:line1 else echom "Substitution on lines" a:line1 "to" a:line2 endif endfunction command -range -nargs=1 S call s:Substitute(<line1>, <line2>, <q-args>)

Hope that helps someone else, and thanks for the earlier function I used to build this! (I could only test on linux, btw) --Peter Kay

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.