Thursday, January 28, 2010

Re: user function for multiple substitution

On Thu, Jan 28, 2010 at 9:43 PM, bill lam <cbill.lam@gmail.com> wrote:
> Now I use bash script sed to convert esperanto x-system to unicode
>
> #!/bin/sh
> sed -e "s/cx/ĉ/g" -e "s/gx/ĝ/g" -e "s/hx/ĥ/g" ... ktp
>
> so that :%!xeo to convert whole file or visual hilite region then :!seo
>
> How to define a user function Xeo to do the same thing?

A quick literal translation of that into a vim command would be this:

---

function! s:Xeo()
silent s/cx/ĉ/ge
silent s/gx/ĝ/ge
silent s/hx/ĥ/ge
endfunction

command! -range Xeo :<line1>,<line2> call s:Xeo()

---

That just makes a script-local function called Xeo() that replaces all
"cx" on the current line with "ĉ", ignoring errors (the e flag) and
not printing out the number of substitutions made regardless of the
user's 'report' setting (the :silent). Then it repeats that action
for the other patterns. Then, it defines a :Xeo command that can take
a range and calls the s:Xeo() function in a loop, once for each line
of the range, so :'<,'>Xeo or :%Xeo will work.

~Matt

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: