Breaking out of a commandline loop

3 views
Skip to first unread message

BigAl

unread,
Dec 16, 2008, 2:12:15 PM12/16/08
to vim_use
How can I break out of a series of commandline commands from within
the commandline?

Here is my command line, it finds the text "XXXX" and replaces that
text with an incremented number (variable i) up to the max replacement
value of 3999.
:let i=1120|%g/XXXX/s/\zsXXXX\ze/\=i/|let i=i+1|if (i > 3999)|break|
endif

Everything works great except for the "break" statement. When the
"break" is hit it generates an error since break is only intended for
use with a while statement. I've tried putting the "<Esc>" text in
place of the break statement, I've tried inserting a ctrl-c in line...
and I can't make any of them work.

Tim Chase

unread,
Dec 16, 2008, 4:10:16 PM12/16/08
to vim...@googlegroups.com
> Here is my command line, it finds the text "XXXX" and replaces
> that text with an incremented number (variable i) up to the
> max replacement value of 3999.
> :let i=1120|%g/XXXX/s/\zsXXXX\ze/\=i/|let i=i+1|if (i > 3999)|break|
> endif
>
> Everything works great except for the "break" statement. When
> the "break" is hit it generates an error since break is only
> intended for use with a while statement. I've tried putting
> the "<Esc>" text in place of the break statement, I've tried
> inserting a ctrl-c in line... and I can't make any of them
> work.

I think with a little rejiggering, you can move the "if" to the
front:

:let i=1120
:g/XXXX/if i < 4000 | s/\zsXXXX\ze/\=i/ | let i=i+1 | endif

You might have to adjust for fence-posting errors I may have
introduced. The :g will still visit each line, but if the
threshold is passed, no action is taken on those lines.

-tim

Tony Mechelynck

unread,
Dec 16, 2008, 4:20:23 PM12/16/08
to vim...@googlegroups.com

Here's another attempt (untested)

:let i = 1120 | %g/XXXX/s/XXXX/\=(i<4000?i:submatch(0)) | let i += 1

Alternately, you might put a try/endtry block around the argument of the
":g" statement to catch the generated error, see
:help :try
:help :catch
:help :finally


Best regards,
Tony.
--
"Star Wars is adolescent nonsense; Close Encounters is obscurantist
drivel; Star Trek can turn your brains to purée of bat guano; and the
greatest science fiction series of all time is Doctor Who! And I'll
take you all on, one-by-one or all in a bunch to back it up!"
-- Harlan Ellison

BigAl

unread,
Dec 17, 2008, 5:11:25 PM12/17/08
to vim_use
Guys... thanks for the suggestions...

Tim your suggestion is working great... can't believe I didn't think
of that.

Tony... I tried using the submatch but it doesn't look like the
submatch function is supported in our vim installation. Also hadn't
thought of a try catch finally... something to keep in mind for future
stuff.

On Dec 16, 3:20 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:

Tony Mechelynck

unread,
Dec 17, 2008, 11:10:30 PM12/17/08
to vim...@googlegroups.com
On 17/12/08 23:11, BigAl wrote:
> Guys... thanks for the suggestions...
>
> Tim your suggestion is working great... can't believe I didn't think
> of that.
>
> Tony... I tried using the submatch but it doesn't look like the
> submatch function is supported in our vim installation. Also hadn't
> thought of a try catch finally... something to keep in mind for future
> stuff.

submatch() can only be used in a |sub-replace-expression| -- an
expression given after \= in the "replace by" string of a
":s[ubstitute]" command. Otherwise I would expect it to be available in
any Vim version compiled with expression evaluation. IOW, if you don't
have it, you probably have a crippled-down Vim ("Small" or "Tiny", see
near the top of the output of ":version"). In that case I would
recommend to install a better-equipped version of Vim (vim-enhanced
and/or vim-x11 in a RedHat-like Linux distribution; Steve Hall's
https://sourceforge.net/project/showfiles.php?group_id=43866&package_id=39721
on Windows; or compile it yourself as a Big or Huge build, see the two
HowTo pages on my Vim site http://users.skynet.be/antoine.mechelynck/

Best regards,
Tony.
--
Reporter (to Mahatma Gandhi): Mr Gandhi, what do you think of Western
Civilization?
Gandhi: I think it would be a good idea.

BigAl

unread,
Dec 18, 2008, 8:45:05 AM12/18/08
to vim_use
I was using it as part of a sub-replace-expression (I copied your
commandline example to test it in my edit session).

We're running vim 6.1, huge version w/o gui... and this is a corporate
installation. I have no control over the configuration so what I got
is what I got. Thanks for all of the info though... didn't know about
the :version command either.

Al
On Dec 17, 10:10 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
> On 17/12/08 23:11, BigAl wrote:
>
> > Guys... thanks for the suggestions...
>
> > Tim your suggestion is working great... can't believe I didn't think
> > of that.
>
> > Tony... I tried using the submatch but it doesn't look like the
> > submatch function is supported in our vim installation.  Also hadn't
> > thought of a try catch finally... something to keep in mind for future
> > stuff.
>
> submatch() can only be used in a |sub-replace-expression| -- an
> expression given after \= in the "replace by" string of a
> ":s[ubstitute]" command. Otherwise I would expect it to be available in
> any Vim version compiled with expression evaluation. IOW, if you don't
> have it, you probably have a crippled-down Vim ("Small" or "Tiny", see
> near the top of the output of ":version"). In that case I would
> recommend to install a better-equipped version of Vim (vim-enhanced
> and/or vim-x11 in a RedHat-like Linux distribution; Steve Hall'shttps://sourceforge.net/project/showfiles.php?group_id=43866&package_...
> on Windows; or compile it yourself as a Big or Huge build, see the two
> HowTo pages on my Vim sitehttp://users.skynet.be/antoine.mechelynck/
Reply all
Reply to author
Forward
0 new messages