It looks like you want to follow the recommendation on http://dustycloud.org/blog/vcs-friendly-patchable-document-line-wrapping/. This answer offers a solution to apply these rules to several paragraphs in one step.
During writing I would simply recommend, setting textwidth=80
, expandtab
, shiftwidth=2
and press tab when you are in a new line and the sentence is not finished. Auto-insertion of two spaces in new lines often leads to situations where you do not want to have this, e.g.
\section{Section 1}<CR>
CURSOR UNINTENTIONALLY INDENTED
So back to the situation where you want to fix the indentation of several paragraphs which are not properly indented according to this style (typically after modifying the text):
Since sentence detection is quite difficult, one could use a machine learning toolkit (e.g. Apache OpenNLP):
$ cat dusty.txt
If you do enough work in any sort of free software environment, you get used to
doing lots of writing of documentation or all sorts of other things in some
plaintext system which exports to some non-plaintext system. One way or
another you have to decide: are you going to wrap your lines with newlines?
And of course the answer should be "yes" because lines that trail all the way
off the edge of your terminal is a sin against the plaintext gods, who are
deceptively mighty, and whose wrath is to be feared (and blessings to be
embraced). So okay, of course one line per paragraph is off the table. So
what do you do?
$ tr '\n' ' ' < dusty.txt | opennlp SentenceDetector en-sent.bin 2>/dev/null | par 78p2dh
If you do enough work in any sort of free software environment, you get used
to doing lots of writing of documentation or all sorts of other things in
some plaintext system which exports to some non-plaintext system.
One way or another you have to decide: are you going to wrap your lines with
newlines?
And of course the answer should be "yes" because lines that trail all the way
off the edge of your terminal is a sin against the plaintext gods, who are
deceptively mighty, and whose wrath is to be feared (and blessings to be
embraced).
So okay, of course one line per paragraph is off the table.
So what do you do?
vim configuration for the operator gq
:
let &l:formatprg='tr "\\n" " "|opennlp SentenceDetector en-sent.bin 2>/dev/null |par 78p2dh'
This should work for the English language and uses the program http://www.nicemice.net/par/.
For installation details and using alternatively NLTK see https://github.com/lervag/vimtex/issues/544#issuecomment-299444814.
textwidth
option see:h :tw
.set cpo+=J
. This will allow you to jump between sentences with(
and)
, ignoring abbreviation dots.set textwidth=80
to cut your lines when they are longer than 80 characters and then useformatexpr
(:h 'formatexpr'
) which allows to set create a function handling the behavior to follow when a line is wrapped. If you create a function which add two whitespaces to the wrapped line you should be able to do what you want. (I'll try to work on it later today and I'll post something if I get something interesting)