| Computers Forum Index » Computer - Editors » replace x times... |
|
Page 1 of 1 |
|
| Author |
Message |
| julia... |
Posted: Thu Oct 01, 2009 1:31 pm |
|
|
|
Guest
|
How to replace 5 times {} by {foo}
\textstyleCblue{String(string),StringString(String,Sring)}&{string}&
{(string)}&{}\tabularnewline
My RE
:%s/\(\text.\{-}\)\{\}\\tabularnewline/\1{foo}\\tabularnewline
Is it possible to make my RE more specific in telling vim that I want
to do the replace only 5 times.
Thanks |
|
|
| Back to top |
|
|
|
| Peter Gordon... |
Posted: Fri Oct 02, 2009 5:16 am |
|
|
|
Guest
|
julia <julia_2683 at (no spam) hotmail.com> wrote in news:2438f30a-06df-47d4-8013-
96f27f0e3eb5 at (no spam) d4g2000vbm.googlegroups.com:
Quote: How to replace 5 times {} by {foo}
\textstyleCblue{String(string),StringString(String,Sring)}&{string}&
{(string)}&{}\tabularnewline
My RE
:%s/\(\text.\{-}\)\{\}\\tabularnewline/\1{foo}\\tabularnewline
Is it possible to make my RE more specific in telling vim that I want
to do the replace only 5 times.
Thanks
I'm new to vim scripting and this question got
me interested.
With the following script in my _vimrc file
function Change( count)
1
let i = 0
while i < a:count
s/c/X/
+
let i = i + 1
endwhile
endfunction
When executed with a :call Change(5)
the script changed the first occurance of "c"
to "X" on the first 5 lines.
However, if one of the lines does not contain a
"c", the script produces an error and only four
changes are made.
Question 1:
Is there a method of testing the substitute command?
Something like:
if( s/c/X/ == 0)
+
endif
Question 2:
Can the error message be supresed?
Question 3:
How can the script be located in another file
and then loaded and called when required?
Peter Gordon |
|
|
| Back to top |
|
|
|
| Christian Brabandt... |
Posted: Fri Oct 02, 2009 9:31 am |
|
|
|
Guest
|
On 2009-10-02, Peter Gordon <petergo at (no spam) XYZnetspace.net.au> wrote:
Quote: function Change( count)
1
let i = 0
while i < a:count
s/c/X/
+
let i = i + 1
endwhile
endfunction
When executed with a :call Change(5)
the script changed the first occurance of "c"
to "X" on the first 5 lines.
Well, this can be replaced to 1,5s/c/X/. It's harder if you want to
replace at most 5 occurences per line, but I can't think of a solution
for that right now without creating a complicated script.
Quote:
However, if one of the lines does not contain a
"c", the script produces an error and only four
changes are made.
Question 1:
Is there a method of testing the substitute command?
Something like:
:h search()
Quote: if( s/c/X/ == 0)
+
endif
Question 2:
Can the error message be supresed?
:h s_flags and look for e
Quote: Question 3:
How can the script be located in another file
and then loaded and called when required?
There are various ways to achieve this. You could use filetype
plugins, global plugins and autocommands. There are probably more
ways, I can't think of right now.
See :h filetype-plugin
:h add-global-plugin
:h autocommand
regards,
Christian |
|
|
| Back to top |
|
|
|
| Michael Paoli... |
Posted: Sat Oct 03, 2009 5:53 am |
|
|
|
Guest
|
On Oct 1, 6:31 am, julia <julia_2683 at (no spam) hotmail.com> wrote:
Quote: How to replace 5 times {} by {foo}
echo '{}{}{}{}{}{}{}{}{}{}' |
sed -e 's/{}/{foo}/;s/{}/{foo}/;s/{}/{foo}/;s/{}/{foo}/;s/{}/{foo}/'
{foo}{foo}{foo}{foo}{foo}{}{}{}{}{}
echo '
{}
{}{}{}
{}{}{}{}{}
{}{}{}{}{}{}{}
' |
sed -e ':t;s/{}/{foo}/;ts;P;d;:s;/\n\n\n\n$/{;P;d;};s/$/\n/;bt'
{foo}
{foo}{foo}{foo}
{foo}{foo}{foo}{foo}{foo}
{foo}{foo}{foo}{foo}{foo}{}{} |
|
|
| Back to top |
|
|
|
|