Friday, February 1, 2013

Re: Migrating text from vim to wysiwyg editor

On Fri, 1 Feb 2013 20:55:09 -0700, Chris Schneider wrote:
> Tim - can you describe what to search help on for that trick?
> Second time I've seen it today, with the :g/pattern/range -dash-
> command.

Think of it as

:{range}g/{pattern}/{ex command}

where "{ex command}" can include a range relative to the line the
match was found on. So you can do things like find every line
containing "foo" and indent the line above through the line below:

:g/foo/-,+>

That could also be written more explicitly as

:g/foo/-1,+1>

or make it less annoying (since ">" reports every set of lines it
shifts) with

:g/foo/sil! -,+>

Here my Ex command is ">" (shift a range right by one 'shiftwidth').

My relative range is "-,+" (more explicitly "-1,+1", but since it
defaults to 1 if you don't specify, I often don't). By using the
comma, it moves the start of the range back one line, but then
adjusts the end of the range forward one line *from the line that
matched*. If instead I'd used "-;+", it would have been back one
line, and then forward one line relative to that. Things get really
crazy when you start to stack movements such as

:g/foo/?bar?+2;/baz/-1d

which searches for lines matching "foo". On each matching line, it
then searches backwards for "bar", then moves forward two lines to
start the range. Then, from that point (rather than the initial
"foo"-matching line, as it would if I'd used a comma instead of a
semicolon), it searches forward until it finds "baz" and then backs
up the end of the range by one line. With that range in hand, it
then deletes the range in question (":help :d"). You can read a
brief description of the range specifiers at

:help :range

It's this sort of crazy power that makes it hard for me to use any
other editor for any length of time. "What do you mean that your
editor doesn't have an easy way to perform substitutions on the
five lines following the first instance of 'foo' after instances
of 'bar'?! Vim does…" :)

-tim

:help :range
:help :g
:help ex-cmd-index












--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment