Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Editing and Text Objects

Vim Change, Delete, Replace and Put

Learn the everyday editing commands: x, dd, r, s, cw, cc, C, o and O, plus put with p and P, swapping text and the difference between change and replace.

This lesson collects the editing commands you will press all day. Many are shortcuts for an operator plus a motion, so you already know the logic; the goal here is to know which one to reach for in each situation and avoid the common slips.

Deleting

Command Effect
x delete the character under the cursor (dl)
X delete the character before the cursor (dh)
dw delete to the next word
dd delete the line
D delete to end of line
dip delete a paragraph
d/text<Enter> delete up to the next match

A count makes them stronger: 5x deletes five characters, 3dd three lines. Everything you delete is saved in a register, so you can paste it later; see registers. That is why Vim's "delete" is really "cut".

Changing

Change deletes text and drops you into Insert mode in one move, so you type the replacement immediately.

Command Effect
cw change to the end of the word
ciw change the whole word
cc or S change the whole line (keeps indentation with autoindent)
C change to end of line
s substitute one character (cl)
ct, change until the next comma
ci" change inside quotes

Notice that cw behaves like ce: it stops at the end of the word, not the start of the next, which is a deliberate special case. When in doubt, ciw is clearer.

The workflow for fixing a typo word is: move onto it (w, b, f), ciw, type the new word, Esc. Then for another occurrence, move and press ..

Replacing without Insert mode

r{char} replaces the character under the cursor with another and stays in Normal mode. 3rx replaces three characters with x. To replace with a newline use r<Enter>, which splits a line, a neat way to break a long line at a comma: f, r<Enter>. Capital R is the Replace mode that overwrites as you type, ended by Esc.

gr variants replace virtually (they respect tab width), but you will rarely need them.

Opening and joining lines

  • o opens a line below and enters Insert mode; O opens one above.
  • J joins the next line to this one with a space; gJ joins without adding a space.
  • ddp swaps a line with the one below; ddkP moves a line up.
  • xp swaps two characters (typo fix: "teh" becomes "the" with lxp... try it).

Putting text back

p puts (pastes) after the cursor, P before. For linewise text (copied with yy or dd) p puts the lines below the current one and P above. For characterwise text it inserts after or before the cursor. Counts repeat: 3p puts it three times.

  • gp and gP do the same but leave the cursor after the pasted text, handy for repeated pastes.
  • ]p pastes and adjusts indentation to the current line.
  • In Insert mode, Ctrl-r" pastes the unnamed register without leaving the mode.

A common trap

If you delete something (for instance with dd) after yanking, the unnamed register is overwritten and p pastes the deleted text, not what you yanked. The fix is explained in the registers lesson: yank goes to register 0, so "0p pastes the last yank. Remember this and you will save yourself a lot of confusion.

An edit simulator (stand-in)

The block below models the line operations dd, yy, p, P, J, o, O and ddp swaps on an array. It is a stand-in that mirrors the behavior of the commands, with one unnamed register.

JS Line editing commands (stand-in)

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

No output.

Change the commands to explore: what happens to row after dd on the last line, and why does P not move the cursor in this model? (It is a simplification; real Vim leaves the cursor on the new text.)

Replace versus change in one table

A common beginner question is which of r, s, R, c to use. This stand-in prints a recommendation from the size of the job.

JS Which command? (stand-in)

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

No output.

Aim for small, repeatable edits

Each edit becomes a dot-repeatable unit. Prefer one sentence (ciw + text) over a chain of arrow keys and backspaces, because . will redo it. If you make a change with several separate steps, such as delete, move, insert, the dot only repeats the last step. Design each edit to be a single operator command or a single Insert session.

Insert-mode corrections such as Ctrl-w (delete word) are also fine inside one session; they do not split the undo or break the repeat.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does C do?

    C is the shortcut for c$: delete to end of line and enter Insert mode.
  2. 2. How do you replace the character under the cursor and stay in Normal mode?

    r{char} replaces one character without entering Insert mode.
  3. 3. You yanked a line, deleted another, and p pastes the deleted line. Why?

    d and c also write to the unnamed register. Yanks are kept in register 0, so use "0p.
  4. 4. Which sequence swaps the current line with the next one?

    dd cuts the line and p puts it below the following line, which swaps them.

Summary

  • x dd D delete; cw ciw cc C s change; r and R replace.
  • o and O open lines; J joins; ddp and xp swap.
  • p and P put after and before; gp leaves the cursor after the text.
  • Deletes overwrite the unnamed register, so use register 0 for the last yank.
  • Make each edit one dot-repeatable command.

Next: Undo, Redo and the Dot Command