4

I've got the following sample HTML code:

<html><body>
<table class="foo">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
</table>
<table class="bar">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
</table>
</body></html>

and I'd like to sort the second table in reverse.

However whenever I run this command at the beginning of the file:

:/<table class=.bar\_.\{-}\zs<tr/,/table>/sort! n /.\{-}<td/

I've got the user interactive warning saying:

Backwards range given, OK to swap (y/n)?

If I select n - nothing happens, when I select y, then the results are not what I would expect.

Although it works as expected, when I run command by having cursor after the 1st table.

How I can perform the above sorting operation non-interactively without being worried where my cursor is?

1
  • BTW, your example is very valid and realistic but also too complicated (the difference between your command and the right one is barely noticeable). For future questions please consider reducing your example even more.
    – romainl
    Commented Nov 15, 2016 at 10:36

1 Answer 1

8

In :<from>,<to>command, both <from> and <to> are relative to the current line.

What you want is to make <to> relative to <from>. For this you need :help :;:

:/foo/;,/bar/command

where the cursor is moved to /foo/ first, which becomes the current line.

:/<table class=.bar\_.\{-}\zs<tr/;,/table>/sort! n /.\{-}<td/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.