vimtricks.wiki Concise Vim tricks, one at a time.

How do I select, delete, or change a complete sentence using Vim's sentence text objects?

Answer

as and is

Explanation

Vim defines sentence text objectsas (around sentence) and is (inner sentence) — that allow any operator to act on an entire sentence in one motion. These text objects complement the more commonly known word and paragraph objects and are particularly useful when editing prose, documentation, or comment blocks.

How it works

  • is selects the inner sentence: the sentence text itself, without leading or trailing whitespace
  • as selects the around sentence: the sentence text plus any trailing whitespace that separates it from the next sentence
  • A sentence boundary is defined as a ., !, or ? followed by whitespace or the end of the line (configurable via :help sentence)
  • These objects combine with any operator: d, c, y, g~, gU, gu, >, etc.

Example

Given the text (cursor anywhere on the second sentence):

This is the first sentence.  It contains a bug.  This is fine.
  • dis — deletes It contains a bug. (leaves surrounding spaces)
  • das — deletes It contains a bug. (includes the trailing space)
  • cis — changes the inner sentence, leaving you in insert mode to retype
  • yis — yanks the sentence without the surrounding whitespace

Tips

  • Use is when you want to retype a sentence from scratch (cis), since as would also delete the space before the next sentence
  • Combine with counts: d2as deletes two sentences at once
  • Works across wrapped lines — a sentence can span multiple lines
  • If your text doesn't use standard sentence endings, consider using paragraph text objects (ip/ap) instead
  • Use ( and ) to jump between sentences without text objects

Next

How do I enable matchit so % jumps between if/else/end style pairs?