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?
: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).
formatexpr
and report back when I get a chance.
:%s/–$/-/
. Out of all the possible options, it's certainly the easiest.
Dec 19, 2016 at 23:27
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?