Text Objects & Motions

Text objects are perhaps the single most important reason to learn Vim. In most editors, manipulating text means tedious sequences of clicking, shift-selecting, and hoping you grabbed exactly the right characters. Text objects replace all of that with precise, repeatable intent: "delete inside these quotes," "change this function's arguments," "copy this paragraph." Once you internalize them, you stop thinking about characters and cursor positions and start thinking about meaning. That shift is what separates someone who uses Vim from someone who thinks in Vim.

Text objects let you operate on structured pieces of text — words, sentences, paragraphs, quoted strings, HTML tags, and more — with just a few keystrokes.

What you'll learn in this chapter:

  • Understand Vim's composable grammar of verbs, modifiers, and text objects

  • Use inner and around text objects to precisely select words, sentences, paragraphs, quotes, and brackets

  • Combine text objects with operators like d, c, and y for efficient editing

  • Manipulate visual selections and understand linewise vs characterwise copy/paste behavior

The Grammar of Vim

Vim commands follow a composable grammar: verb + modifier + object

Component
Role
Examples

Verbs (Actions)

What to do

v visual, c change, d delete, y yank (copy)

Modifiers

How to select

i inside, a around, t till (not included), f find (included)

Text Objects

What to act on

w word, p paragraph, s sentence, " quotes, { braces

This composability is what makes Vim a language for editing text. Once you learn the components, you can combine them freely (see Modes, Navigation & Basic Editing for the foundational motions and operators that text objects build upon).

Think of it like learning to speak a natural language. You don't memorize every possible sentence — you learn verbs ("delete," "change," "yank") and nouns ("word," "paragraph," "the stuff inside these braces"), and then you combine them on the fly. d + aw is "delete a word" the same way you'd say it in English. c + i" is "change inside quotes." The grammar is small, but the sentences you can construct are practically infinite. Every new verb you learn works with every noun you already know, and vice versa — so your editing vocabulary grows multiplicatively, not linearly.

Text objects infographic

Common Text Objects

Text Object
What it means
Example

iw

Inner word (no surrounding space)

diw — Delete inner word

aw

A word (includes trailing space)

daw — Delete a word + space

iW

Inner WORD (no surrounding space)

diW — Delete inner WORD

aW

A WORD (includes trailing space)

daW — Delete a WORD + space

is

Inner sentence

cis — Change inner sentence

as

A sentence

das — Delete a sentence

ip

Inner paragraph

yip — Yank inner paragraph

ap

A paragraph

cap — Change a paragraph

i"

Inner double quotes

di" — Delete inside quotes

a"

A double quotes (includes the quotes)

da" — Delete quotes and content

i'

Inner single quotes

ci' — Change inside single quotes

i( or ib

Inner parentheses

ci( — Change inside parens

a( or ab

A parentheses (includes parens)

da( — Delete parens and content

i{ or iB

Inner braces

di{ — Delete inside braces

i[

Inner brackets

vi[ — Select inside brackets

i<

Inner angle brackets

di< — Delete inside angle brackets

it

Inner tag (HTML/XML)

cit — Change inside tags

at

A tag (includes the tags)

dat — Delete tags and content

f,

Till next comma (included)

cf, — Change till comma

t,

Till next comma (not included)

vt, — Select till comma

Text objects overview

Note: word vs WORD — Vim differentiates word from WORD. A word is delimited by non-keyword characters (letters, digits, underscore). A WORD is delimited only by whitespace. For example, my-variable is three words (my, -, variable) but one WORD. Use iW/aW when you want to operate on hyphenated names, URLs, or file paths. See :help word and :help WORD.

Neovim: With nvim-treesitter-textobjectsarrow-up-right, you get text objects for functions, classes, parameters, conditionals, and more — all language-aware! See Chapter 14: Treesitter.

Real-World Text Object Combinations

Text objects truly shine when combined with verbs. Here are patterns you'll use daily:

Nesting

Text objects respect nesting. If your cursor is inside nested parentheses:

  • ci( changes cursor_here (innermost parens)

  • Pressing ci( again targets the next level up

Unnamed Register and Text Objects

Text objects interact with registers in useful ways (see Registers for a comprehensive guide to all register types). For example, to wrap a word in quotes:

  1. ciw — Change inner word (deleted word goes to unnamed register)

  2. Type ' — Insert opening quote

  3. Ctrl+r" — Paste the word from unnamed register

  4. Type ' — Insert closing quote

Tip: mini.surroundarrow-up-right (Neovim) or vim-surroundarrow-up-right make surrounding operations much easier. With mini.surround: saiw" to surround a word with quotes.

Toggling Words in Normal Mode

To exchange two adjacent words:

  1. yiw — Copy the first word

  2. w — Jump to the next word

  3. viwp — Select and paste over the second word

  4. Go back to the first word

  5. viwp — Select and paste (the unnamed register now has the second word)

Tip: vim-exchangearrow-up-right simplifies this: type cxiw on the first word, then . on the second.

Visual Selection Movement

So far, we have focused on using text objects in operator-pending mode — typing a verb and then a noun. But text objects are equally powerful inside Visual mode. You can visually select a region first and then fine-tune exactly what you have highlighted before acting on it. The following keys help you adjust a visual selection once it is active:

Key
What it does

o

Toggle cursor to the other end of the selection

0

Extend selection to start of line

$

Extend selection to end of line

gg

Extend selection to start of file

G

Extend selection to end of file

Tip: o is especially useful: if you selected too far, press o to move to the other end and adjust.

Copy And Paste: Linewise vs Characterwise

This distinction trips up nearly everyone at some point. You yank a line with yy, press p, and the text lands on a new line below instead of right where you wanted it. Or you copy a word, paste it, and it shows up after the cursor instead of on its own line. The reason is that Vim remembers how text was copied — as a full line (linewise) or as a stream of characters (characterwise) — and uses that context to decide where to put it when you paste. Once you understand this, paste behavior becomes completely predictable instead of an occasional source of confusion.

Understanding the difference between linewise and characterwise operations prevents many "paste surprises":

  • yy: Copies the entire line including the newline character (linewise)

  • 0yg_: Copies from start to last visible character (characterwise)

When you paste a linewise yank with p, it goes to the next line. A characterwise paste goes after the cursor.

Tip: See Chapter 9: Mappings for mappings that create il (inner line) and al (a line) text objects. You can also combine text objects with search commands for powerful editing workflows (see Search, Replace & Regex).

Summary

This chapter explored text objects — Vim's mechanism for operating on structured pieces of text such as words, sentences, paragraphs, quoted strings, and bracket pairs. By combining verbs (d, c, y) with modifiers (i for inner, a for around) and text objects, you gain a composable editing language that grows multiplicatively as you learn new components. We also covered visual selection adjustments, the difference between linewise and characterwise operations, and practical patterns for swapping and wrapping text.

Key takeaways:

  • Vim commands follow a composable grammar: verb + modifier + object (e.g., ci" means "change inside quotes")

  • The i (inner) modifier excludes delimiters, while a (around) includes them — choosing the right one determines whether surrounding characters are affected

  • Text objects respect nesting, so ci( targets the innermost parentheses around the cursor

  • Visual mode combined with text objects lets you preview and fine-tune selections before applying an operator

  • Understanding linewise vs characterwise behavior prevents unexpected paste results when working with yy vs 0yg_

Exercises

  1. Inner vs around — Given the text Hello, "beautiful world"!, place your cursor inside the quotes and practice the difference between di" and da". Undo after each attempt with u.

  2. Change inside braces — Given this code block, change the body of the function to return 0;:

  3. Combine operators with text objects — Given <div class="container">Hello World</div>, use text objects to: (a) yank the text inside the tag, (b) delete the entire tag, and (c) change the tag content.

  4. Swap two words — Given the text first second, swap the two words so it reads second first using only text objects and paste.

  5. Paragraph operations — Create a buffer with three paragraphs separated by blank lines. Use yip to yank a paragraph, move to the end of the file, and paste it. Then use dap to delete a different paragraph entirely.

Last updated

Was this helpful?