vim

vim linebreak after hyphen with gq


Is there some way to configure vim's gq function so that it allows line breaking after a hyphen (in a compound word)? e.g.

twentieth-
century

And by the way, I'm not on my own laptop, but on one of the lab, which runs Windows, so any solution not using programs like par or fmt get bonus points :-) (though I'm also interested in solutions using these tools if this is not possible to do this using only vim -- at least I would be able to do it on my laptop).

Thanks in advance.


Solution

  • For the ASCII hyphen (0x2d), this isn't possible. If you're able to use Unicode, you can use the identically-looking Unicode variant (U+2010, cp. https://en.wikipedia.org/wiki/Hyphen) instead. This can be inserted via Ctrl-V (Ctrl-Q on most Windows installations of Vim), followed by u2010. Or define a digraph for it:

    :digraph -- 8208 " hyphen, U+2010
    

    With

    :set formatoptions+=m
    

    Vim will

    Also break at a multi-byte character above 255.

    Voila! If you need to persist the text as ASCII, you could even write mappings / a wrapper around gq that :substitutes the hyphens back and forth.