I've got the following sample HTML code:

<table class="foo">
<table class="bar">

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?

  • 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
    Nov 15, 2016 at 10:36

1 Answer 1


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 :;:


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.