Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Editing and Text Objects

Vim Operators and Motions in Depth

Go deeper into Vim operators: delete, change, yank, indent, case and format; linewise versus characterwise motions; forcing motion types with v, V and Ctrl-v.

You saw the sentence structure in the first chapter: an operator, then a motion. Now we cover the full set of operators and the rules that decide what region an operator acts on. Understanding the rules removes the occasional surprise when a command takes more or less text than you expected.

The operator list

Operator Action
d delete (and copy into a register)
c change: delete and enter Insert mode
y yank (copy)
> and < shift right and left by shiftwidth
= re-indent with the indent rules
gu gU g~ lowercase, uppercase, toggle case
g? rot13 encode (a fun demonstration)
gq format text to textwidth and wrap
gw like gq but keeps the cursor in place
! filter lines through an external command
zf create a fold

Each one waits for a motion or text object. Short forms exist for common pairs: D is d$, C is c$, Y is yy (in Neovim, Y is y$, a consistent fix), S is cc, s is cl, x is dl, X is dh.

Characterwise, linewise and blockwise

Every motion has a type, and the type determines what region the operator affects:

  • Characterwise motions (w, e, $, f, t, h, l) act on text from the start to the end position. Some are exclusive and some inclusive (see the grammar lesson).
  • Linewise motions (j, k, G, gg, {, }, 'a, H, L) act on all lines they touch, in full. dj deletes the current and next lines entirely, not just up to the same column.
  • Blockwise is reached only through Visual block mode.

One reason d} does what you expect: } is characterwise but Vim applies special handling for motions that end at a blank line (:help o_v). In practice you can think "paragraph".

Forcing a type

Type v, V or Ctrl-v between the operator and the motion to override the motion's natural type:

  • dvj makes j characterwise: it deletes from the cursor to the same column on the next line, not both lines.
  • dVw makes w linewise: deletes the whole line.
  • d<C-v>j makes it blockwise: a rectangle two lines tall.

You will rarely need this, but it is invaluable the day a plain motion is almost right.

Modeling the region (stand-in)

The block below shows the difference between characterwise and linewise application on an array of lines. It is a stand-in for the rule, not Vim itself.

JS Charwise vs linewise delete (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first output keeps parts of both lines joined together; the second removes both lines completely. That is the difference dvj and dj make.

Counts with operators

Counts can sit before the operator, before the motion, or inside a doubled operator:

  • 3dd and d3d both delete three lines.
  • 2d2w deletes four words.
  • 3>> indents three lines.
  • gUU uppercases a line; 3gUU does three.

For linewise ranges using explicit lines, :5,9d deletes lines 5 to 9 from the command line, and :5,9> indents them. We use that style often in chapter 4.

Indenting and formatting

> and < shift by shiftwidth, not by a tab stop: set that in your vimrc (chapter 6). >ip indents a paragraph, >> a line, and in Visual mode > indents the selection and 3> shifts three levels. =ap re-indents a block by the language's rules; gg=G re-indents the whole file, which works best when a filetype indent plugin exists.

gq reflows prose. With textwidth=72, gqap rewraps the current paragraph. gqq formats one line. gw does the same but returns the cursor, so you can keep working.

Filtering through programs

! sends lines through any shell command and replaces them with the output. !ipsort<Enter> sorts the current paragraph; !Gjq .<Enter> formats JSON from here to the end through jq if it is installed. In practice you type ! plus a motion, and Vim opens the command line with a range already filled in. This turns the whole Unix toolbox into editing commands.

Operators and registers

Every d, c and y writes to a register, so what you delete can be pasted: ddp swaps a line with the next one (delete it, paste below). xp swaps two characters. Registers get a full lesson in chapter 4.

Moving by operators and .

Because an operator plus motion is one change, . repeats it. After dw press . to delete another word; after >> press . to indent the next line (move first with j). With gUiw, a . on another word uppercases that word as well. The rhythm "do once, then move and repeat" is the core of Vim productivity, described in the dot-repeat lesson.

Case study: reformat a list

Given a list of lines, uppercase the first word of each:

  1. Put the cursor on the first line.
  2. gUiw uppercases the first word.
  3. j then . repeats the uppercase on the next line.
  4. Hold j. or use a macro (chapter 4) for a long list.

Another approach: Vjjj then :s/^\w\+/\U&/ (chapter 4), but for three lines the dot is shorter.

JS Operator table lookup (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between dj and dvj?

    j is linewise, so dj deletes both lines. Forcing v makes it characterwise.
  2. 2. Which shorthand equals d$?

    D deletes to the end of the line; C changes to the end; S changes the whole line.
  3. 3. What does gg=G do?

    = is the re-indent operator, so gg=G applies it from the first line to the last.
  4. 4. Which operator lets you pipe lines through an external command?

    The ! operator filters the lines of a motion through a shell command and replaces them with its output.

Summary

  • Operators: d c y > < = gu gU g~ gq gw ! and more, each waiting for a motion.
  • Motions are characterwise, linewise or blockwise, which decides how much text is affected.
  • Force a type with v, V or Ctrl-v after the operator.
  • Counts multiply; doubled operators act on lines.
  • =, gq and ! turn Vim into a formatter and a shell front end.

Next: Text Objects: Words, Quotes, Brackets and Tags