Sort lines, then tag, then unsort

44 views
Skip to first unread message

Julius Hamilton

unread,
Sep 14, 2021, 7:58:02 AM9/14/21
to v...@vim.org
I would like to sort lines in a text document and then tag them, and then revert the file to its original order.

First of all, is there a way to tag a line, so that lines with a given tag can be quickly referred to, but the tag is not in the actual text?

Secondly, I noticed with my sort command ":sort \^[A-Z].*[^\.]$\ r" (sort all lines beginning with a capital letter and not ending in a period) it sends the matches in alphabetical order to the bottom of the file. Is there a way to specify a second-order sort by which all the matches are sub-sorted, rather than the default alphabetical? And, is there a way to send them to the top of the document rather than the bottom?

Thanks very much,
Julius

Christian Brabandt

unread,
Sep 14, 2021, 8:17:57 AM9/14/21
to v...@vim.org

On Di, 14 Sep 2021, Julius Hamilton wrote:

> I would like to sort lines in a text document and then tag them, and then revert the file to its original order.
>
> First of all, is there a way to tag a line, so that lines with a given tag can be quickly referred to, but the tag is not in the actual text?

I think this can be done using text-properties.
Have a look at :h text-prop-intro

Best,
Christian
--
Ungleich den Franzosen und Engländern, loben die Deutschen nichts (an
einem Autor, Menschen), ohne alles zu loben; sie glauben parteiisch
sein zu müssen.
-- Jean Paul

rwmit...@gmail.com

unread,
Sep 14, 2021, 8:52:33 AM9/14/21
to vim_use
Interesting, I did not know about text properties although it only seems marginally useful:

```When a buffer is unloaded, all the text properties are gone.  There is no way
to store the properties in a file.  You can only re-create them```

What would it take to save them to a shadow file?

rwmit...@gmail.com

unread,
Sep 14, 2021, 8:55:34 AM9/14/21
to vim_use
To be fair, I was thinking of ad-hoc creating properties, not using a script parsing a file.

Paul

unread,
Sep 19, 2021, 10:29:26 AM9/19/21
to vim...@googlegroups.com
On Tue, Sep 14, 2021 at 01:57:34PM +0200, Julius Hamilton wrote:
>First of all, is there a way to tag a line, so that lines with a given tag
>can be quickly referred to, but the tag is not in the actual text?

Will marks suffice?

王子陌

unread,
Sep 21, 2021, 3:02:38 AM9/21/21
to vim...@googlegroups.com, v...@vim.org
I think the plugin you need is https://github.com/kshenoy/vim-signature

Christian Brabandt <cbl...@256bit.org> 于2021年9月14日周二 下午8:17写道:
--
--
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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20210914121742.GB2645325%40256bit.org.


--

Julius Hamilton

unread,
Oct 4, 2021, 5:52:44 AM10/4/21
to vim...@googlegroups.com


On Sun 19. Sep 2021 at 16:29, 'Paul' via vim_use <vim...@googlegroups.com> wrote:


Will marks suffice?

Thanks for your message.

Not sure, can you apply the same mark, i.e. “h”, to multiple lines? I don’t think so but I’m not sure.

Paul

unread,
Oct 5, 2021, 6:02:15 AM10/5/21
to vim...@googlegroups.com
No, you can't, but there are a limited number of marks available.

Steve Litt

unread,
Oct 10, 2021, 4:25:32 AM10/10/21
to vim...@googlegroups.com
Julius Hamilton said on Tue, 14 Sep 2021 13:57:34 +0200
I think you're using the wrong tool. Also, you haven't said why you
need Vim tags on lines. If I'm not mistaken, there are only a limited
number of tags, and if you have that few lines, you're better off doing
this all manually.

Anyway, the tool I'd use is AWK. Not one liners, but real AWK programs.

My first step would be to put every about 3 underscores followed by
the line number at the end of every line, but add one million to
the line number so all line numbers have the same length. This is
trivial in AWK. Now you have a record of the original sort order.

Next, I'd perform whatever kind of two stage or branched sorting you
need to do, once again with AWK.

Next, do whatever you need to do in order to simulate Vim tags. Because
you haven't told us what the tags are used for, I can't comment further.

Next, do whatever you need to do with those simulated tags.

Finally, within Vim, move the ending line numbers to the front of each
line, and re-sort again, to put it in the original order. Finally, use
Vim to get rid of those line numbers.

SteveT

Steve Litt
Spring 2021 featured book: Troubleshooting Techniques of the Successful
Technologist http://www.troubleshooters.com/techniques
Reply all
Reply to author
Forward
0 new messages