6

When writing LaTeX, I like a newline between every sentence as it helps me jump around more easily. Additionally, I put two spaces at the start of lines where sentences don't start. For example:

This is the first sentence, and after 80 characters or so
  it wraps like this.
A new sentence looks like this.

Any idea for how to automate this in Vim?

  • For the line break at the 80 column you could look at textwidth option see :h :tw. – vappolinario Mar 15 '16 at 18:50
  • You could also put two spaces after full-stops, and set cpo+=J. This will allow you to jump between sentences with ( and ), ignoring abbreviation dots. – Toothrot Mar 15 '16 at 20:15
  • 1
    I think you'll have to use set textwidth=80 to cut your lines when they are longer than 80 characters and then use formatexpr (: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) – statox Mar 16 '16 at 12:38
1

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.

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.