4

Suppose I have a bunch of folds for more than one line each. How to make shuffle them as folds not lines?

In case of lines it can be done by external shuf or any other command (see https://stackoverflow.com/a/17643450/359866 for example).

So the question is for more general case of multiline folds.

P.S. Example.

Unfolded:

fold1
  subfold text1
fold2
  subfold text2
fold3
  subfold text3

Folded:

fold1...
fold2...
fold3...

After shuffle for example

fold2
  subfold text2
fold1
  subfold text1
fold3
  subfold text3
  • I think this kind of task would be more fitted for awk rather than vim: In awk it should be easy to define how your fold works to get each section and then printing them randomly should be doable. With Vim it should be doable too but I think that wouldn't be very elegant to write in Vimscript. – statox Jul 29 at 12:28
  • 1
    @statox, in this case I should re-implement folding (for each file-type) in awk which isn't what I want. I think that in that sense it much more reliable implementation is vimscript folding implementation. – petRUShka Jul 29 at 12:42
  • Of course it has its drawback, I wasn't arguing that you shouldn't do it in Vim, simply that maybe you had more efficient alternative solutions :) – statox Jul 29 at 12:45
6

There is a :folddoclosed command which iterates over all closed folds (just like :g command for the whole buffer) and executes one or more commands on them. So I would close all folds, convert each fold into one long line by replacing the end-of-line with a unique marker, shuffle the lines around, and replace the markers with line-breaks. Something along

zM
:folddoclosed s/\n/<EOL>/
:DoTheShuffle
:%s/<EOL>/\r/g

Depending on the original text and the shuffling method some cleanup might be necessary afterwards, e.g., removal of superfluous empty lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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