0

Description

I have many large size files may including the same function definition.
The block description is start with .start .* and end with .end
In this example there is one duplicate definition .start aa aa
My purpose is to make the duplicated description unique.
BTW, I am using Windows Vim, there's no Linux command uniq to use.

.start aa aa
aaaaa
bbbbb
.end

.start bb cc
aaaaa
bbbbb
.end

.start aa aa                   <---- duplicated description from here
aaaaa
bbbbb                       
.end                           <---- to there

My traditional solution is:

  1. Find the every block and replace the \n to \t as a marker in order to make them into one line :g/\v\.start/,/\.end/s/\n/\t/g
  2. Sort the line and remove the duplicated keep unique :sort u
  3. Finally replace the marker \t to \n make the line back to original block style :g/\v^\.start.*end/s/\t/\r/g

Question

Refer to these two answers
How can I sort multiline blocks in vim?
https://stackoverflow.com/questions/46660252/is-it-possible-to-sort-a-groups-of-lines-in-vim/46660877#46660877
I found a helpful and easy way to sort the block by group or by header

So is there a more efficient way than my traditional solution to keep the same definition block unique?

1 Answer 1

0

Some details of your implementation could be improved but the general strategy is sound.

:g/\.start/,/\.end/s/\n/\t
:sort u
:g/\.start/s/\t/\r/g
  • No need for \v.
  • Some patterns can be simplified.
  • No need for /g on the first command since there is only one \n per line.

The first command may also be simplified to:

:g/\.start/,'}-s/\n/\t

See :help '}.

Note that :sort u kind of implies that you only have those blocks in the buffer, which may or may not open the door to undesired side-effects if that's not the case (losing single line comments, messing with multiline comments, etc.).

If that's too much work, several plugin-based solutions have been suggested in the linked Q/As.

3
  • Where'e the linked Q/As?
    – M_Sea
    Nov 11, 2023 at 1:03
  • In… your question?
    – romainl
    Nov 11, 2023 at 10:05
  • lol, I misunderstand.
    – M_Sea
    Nov 13, 2023 at 2:05

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.