5

I have seen this very interesting article: http://dustycloud.org/blog/vcs-friendly-patchable-document-line-wrapping/ --- basically it is a suggestion for hard-breaking lines of text (for example, in markdown or LaTeX sources) to achieve both easy readability and easy "diff"-ability.

Basically, when I write in LaTeX normally I follow the classical way of "one line per phrase" (hard new line at full stop), and the mandatory blank line for paragraph. This is unfortunately almost unreadable in a diff of a very long phrase, especially if the change is near the end.

The suggested format is to have a line format (a la gqip) for every phrase, but keeping the phrases on their own. For example:

This is a short phrase. This, instead, is a very long, probably too long for English, phrase, but in other languages very long phrases are quite common, really --- and so on and so on.

should be formatted as (this has setl tw=60 to be more dramatic)

This is a short phrase. 
This, instead, is a very long, probably too long for
English, phrase, but in other languages very long phrases
are quite common, really --- and so on and so on.

I can go from the first form to the final one by hand:

  1. format the paragraph with gqip;
  2. go to the first full stop (or question mark, or a symbol meaning the end of the sentence);
  3. if it is the end of the paragraph, stop;
  4. otherwise, insert a linebreak;
  5. then go to the next line, format again the rest of the paragraph as in point 1 above (still searching how to do this effectively --- clearly gqip do not work here unless I temporarily add a blank line above);
  6. repeat until the end of the paragraph.

Is there a way to apply such a type of formatting automatically to a paragraph?

  • Hmm... vi.stackexchange.com/questions/2846/… is very similar... – Rmano Dec 31 '15 at 11:36
  • I don't quite understand what the question is here... – Karl Yngve Lervåg Jan 12 '16 at 17:35
  • @KarlYngveLervåg, you are right. Explained better now, I hope. – Rmano Jan 12 '16 at 18:17
  • 2
    Ok, thanks. I understand the question better now. I don't have the answer, but I have some hints. Check out :h formatexpr - you can define a custom function for the gq command. Also check out :h formatoptions, which can be used to apply the formatter automatically for various scenarios. – Karl Yngve Lervåg Jan 14 '16 at 8:37
3

Rather than changing the way text is actually stored by writing a custom 'formatexpr', I'd be tempted to just to use Vim's existing features to solve the stated goals of easy readability and easy "diff"-ability.

Long lines are easily readable in Vim with the following set:

:set wrap
:set linebreak

And changes towards the end of a line are also perfectly readable when using Vim for diffing (:help diff), so long as you ensure 'wrap' is switched on while you view the diff. (It's off by default).

There's at least one plugin (cvsmenu) that allows you to use Vim for viewing CVS diffs.

If you need to diff outside of Vim, there are tools that can help with this too: e.g. wdiff, or git diff's --word-diff option.

  • 1
    This is a good suggestion, but I really like my text physically formatted --- so that I can have a glance using less, or cat it, or whatever... – Rmano Jan 15 '16 at 12:17
  • @Rmano Hopefully someone will be along soon to explain how you can get a more direct solution working with 'formatexpr' (Or you figure it out for yourself.) – Rich Jan 15 '16 at 12:35

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.