5

I write my email in vim, which dynamically inserts newlines after 72 characters.

formatoptions=awtcql

This works nicely, breaking at spaces. However, is there a way to make it break at hyphens as well?

3

:help 'textwidth' is documented as:

Maximum width of text that is being inserted. A longer line will be broken after white space to get this width.

Reading :help fo-table, it seems there is only one option to change this:

m       Also break at a multi-byte character above 255.  This is useful for
        Asian text where every character is a word on its own.

Which brings us to a possible workaround – you can use the en dash (U+2013) or another non-ascii character instead of a normal hyphen (U+002D):

:set formatoptions+=m
:inoremap - –

It's a bit ugly, but may work in your case.


A second – more complicated – solution can also be found in :help textwidth:

When 'formatexpr' is set it will be used to break the line.

See :help 'formatexpr' for some details. Personally, I've never been able to get this to work quite right for "live formatting" (as opposed to gq formatting), but it should be possible (I never tried very hard).


And lastly, you can use :%! or 'formatprg' to run an external command over the text. This is not ideal as you'll have to run this manually every time, but it's an option (you could also just write a Vimscript command for this though).

  • 1
    I'm tempted to downvote for suggesting that en dashes could replace hyphens. :p However, +1 for the three thorough suggestions. I'm on vacation at the moment, but I'll test out formatexpr and report back when I get a chance. – Sparhawk Dec 18 '16 at 9:46
  • @Sparhawk Yeah, I did say it's ugly ;-) Note that you could use any character above 255, and that you can easily replace them with hyphens with something like :%s/–$/-/. Out of all the possible options, it's certainly the easiest. – Martin Tournoij Dec 19 '16 at 23:27
  • Haha… okay then. I'd prefer not to do that, because it's not automatic. i.e. I'd type with the wrong character, then have to remember to substitute, then what if I wanted to add words somewhere? (And if I did use en-dashes in proper locations, I'd accidentally sub them out.) I also had a look at formatexpr, but I don't really understand how to use it. Also, it's empty by default, and says "When this option is empty 'formatprg' is used." Hence, I wonder if either of these could be tweaked to break on hyphens instead? – Sparhawk Dec 23 '16 at 23:07

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.