C.W.K.
Stream
Lesson 07 of 08 · published

Basic Editing Commands

~12 min · vim, editing, yank-paste

Level 0Trapped
0 XP0/35 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Vim's editing verbs

Three letters carry most of the weight: d for delete, y for yank (copy), c for change. They each compose with motions (Track 2) and text objects (Track 2) into a small language. Today we just memorize the standalone commands.

Deleting

In Vim, deleting and cutting are the same operation — deleted text lands in the unnamed register, ready to be pasted. There is no separate "cut" keystroke, because it would be redundant.

Yanking and pasting

y yanks (copies) without removing. p pastes after the cursor; P pastes before. Linewise yanks (like yy) paste on a new line; characterwise yanks paste inline. Vim figures out which behavior you want from how you yanked.

Change vs delete

c deletes and drops you into Insert mode, ready to type the replacement. cw changes the next word; cc changes the entire line; C changes from cursor to end of line. Whenever you'd type "delete this, then start inserting," reach for change instead.

Undo and redo

u undoes one change. Ctrl-r redoes the last undo. Vim's undo tree is per-buffer and survives across saves; with set undofile (Track 4) it survives across Vim restarts too. U undoes all changes on the current line — useful for "forget I touched this line."

Verbs compose. d + w = dw (delete word). y + $ = y$ (yank to end of line). c + i" = ci" (change inside quotes). The whole of Track 2 is what happens when you take this principle seriously.

The little verbs you'll use every minute

x deletes the character under the cursor (think of it as dl with one keystroke). r replaces a single character — press r then the new character. ~ toggles case. J joins the current line with the next, intelligently inserting a single space.

Code

Delete commands·vim
x      " delete character under cursor
X      " delete character before cursor (backspace-like)
dd     " delete entire line
D      " delete from cursor to end of line
dw     " delete to start of next word
d$     " delete to end of line (same as D)
d0     " delete to start of line
dG     " delete from current line to end of file
dgg    " delete from current line to start of file
Yank and paste·vim
yy     " yank entire line
Y      " yank entire line (same as yy)
yw     " yank to start of next word
y$     " yank to end of line
yiw    " yank inner word (Track 2 preview)
p      " paste after cursor / below current line
P      " paste before cursor / above current line
"+y   " yank into the system clipboard register
"+p   " paste from the system clipboard
Change — delete and enter Insert·vim
cw      " change word
cc      " change entire line
C       " change from cursor to end of line
ciw     " change inner word
ci"    " change inside double quotes
ci(     " change inside parentheses
Undo and the friends nobody talks about·vim
u       " undo one change
Ctrl-r  " redo
U       " undo all changes on the current line (toggle)
:undolist  " show the undo tree (advanced)
g-      " earlier in the undo tree (by time)
g+      " later in the undo tree

External links

Exercise

Open any code file. Without using Insert mode at all, do the following: (1) delete a function body line by line using dd, (2) duplicate a line by yanking with yy and pasting with p, (3) swap two adjacent lines using one dd and one p, (4) join three lines into one using two presses of J. The discipline of not using Insert mode forces you to feel the verb-and-motion language.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.