Line 67: | Line 67: | ||
---- |
---- |
||
+ | |||
+ | === eregex.vim === |
||
There is a plugin [http://www.vector.co.jp/soft/unix/writing/se265654.html 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. |
There is a plugin [http://www.vector.co.jp/soft/unix/writing/se265654.html 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. |
Revision as of 03:03, 10 January 2010
1. Verify with :ver that +perl or +perl/dyn is compiled in.
2. Install Perl if necessary. On Windows, ActivePerl is required.
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):
: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 find 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 brackets () 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/<CR>). I get ^@ character instead of a line break. Yeah, i asked for it but not what I wanted.
In this particular case, just use the old "perl pie":
:%!perl -pi -e 's/<text>/\n/'
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
Move files as the following locations
$HOME/.vim/plugin/eregex.vim $HOME/.vim/plugin/eregex_e.vim $HOME/.vim/doc/eregex_j.txt
SETTING
Add the following code to .vimrc file.
nnoremap / :M/ nnoremap ,/ /
Now you can use / to find. :%S// (uppercase S) to replace.