sponsor Vim development Vim logo Vim Book Ad

vim-sentence-chopper : version-control friendly formatting of prose (and LaTeX markup)

 script karma  Rating 0/0, Downloaded by 736  Comments, bugs, improvements  Vim wiki

created by
Enno Nagel
script type
This Vim plug-in furnishes

- an operator mapping gw in normal mode, for example gwip operates on a
- a mapping gww (and gwgw) in normal mode that operates on a single line,
- a mapping gw that operates on the visual selection, and
- a command ChopSentences that operates on given range (equal to the whole
  buffer if unspecified)

that put each sentence onto a single line.

That is, lines are only broken at punctuation marks .;:?! for better version
control of prose such as markdown, tex or text files.

For best results, ensure that latexindent is installed and the folder that
contains its executable is listed in $PATH (respectively in %PATH% on
Microsoft Windows).
This formatter will also distinguish TeX markup from prose to preserve
syntactic line breaks.

# Usage

For example, hitting gwip on

  Hello! How are you? I'm fine. And you?

turns it into

  How are you?
  I'm fine.
  And you?

# Configuration

The variable g:punctuation_marks defines punctuation marks at which sentences
are split up other than '.'; it is by default set to

  let g:punctuation_marks = '?!'

The variable g:latexindent defines whether latexindent should be used by
default; it is by default set to

  let g:latexindent = 1

The variable g:latexindent_options defines the command-line options passed to
latexindent as listed by latexindent -h ; it is by default set to

  let g:latexindent_options = ''

The variable g:latexindent_yaml_options defines the documented YAML options
passed to latexindent; it is by default set to

  let g:latexindent_yaml_options = ''

For example, to split up sentences after . or ; or : instead of . or ? or !,
and put every sentence in parentheses onto its proper line, add to your vimrc

let g:latexindent_yaml_options = 'modifyLineBreaks:oneSentencePerLine:sentencesEndWith:'
       \ . 'other: \;|\:' . ';'
       \ . 'questionMark: 0' . ';'
       \ . 'exclamationMark: 0' . ','
       \ . 'modifyLineBreaks:oneSentencePerLine:' . 'sentencesFollow:other: "\)"' . ','
       \ . 'modifyLineBreaks:oneSentencePerLine:' . 'sentencesBeginWith:other: [a-z]'

# Mappings

To change mappings, for example, to use zy instead of gw, add the lines

  nmap zy <plug>(ChopSentences)
  xmap zy <plug>(ChopSentences)

to your vimrc.

# Hints

To replace (or delete, or any other operation by Vim) up to the next punctuation mark, say . and ,;:!?, add

  onoremap <silent> . :<c-u>call search('\v\C%(%([^[:digit:]IVX]\|[)''"])\zs[.]\|[,;:!?])[[:space:])''"]\|[.,;:!?]$','W')<CR>

to your vimrc! Then, for example, hitting "c." will change and "d." delete the
text up to the next punctuation mark. Say "c." turns "That's*it, Sir.",
where "*" stands for the cursor position, into "That's*, Sir".
This can be though of as a counterpart to Vim's built-in C and D commands
for prose.

To normalize Unicode Homoglyphs, for example, of white spaces and punctuation
marks, before chopping sentences, see the Vim plug-in vim-unicode-homoglyphs
that highlights and normalizes Unicode homoglyphs.

# Issues

vim-sentence-chopper is developed at https://github.com/Konfekt/vim-sentence-chopper where issues can be filed.
install details
Unzip vim-sentence-chopper.zip into  ~/.vim on Linux or %USERPROFILE%\vimfiles on Windows.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
vim-sentence-chopper.zip 1.5 2024-03-07 7.0 Enno Nagel refactor sentence end detection regex
vim-sentence-chopper.zip 1.4 2022-10-09 7.0 Enno Nagel more robust sentence detection
vim-sentence-chopper.zip 1.3 2021-12-19 7.0 Enno Nagel more robust sentence detection
vim-sentence-chopper.zip 1.2 2019-10-19 6.0 Enno Nagel fix oversights
vim-sentence-chopper.zip 1.1 2019-09-02 6.0 Enno Nagel refinements
vim-sentence-chopper.zip 1.0 2019-07-10 7.0 Enno Nagel Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github