Vim - Change, Delete, Yank

Updated: 2020-06-29

Change(c), delete(d), and yank(y, i.e. copy) share the same use pattern:

c: Change

Change means delete some characters and enter insert mode so you can type some thing else

  • cw: To the end of a word
  • c2b: Back two words
  • c$, C: To the end of line
  • c0: To the beginning of line
  • caw: change the whole word, symbols count as separate word, a,b,c=> ,b,c
  • caW: change the whole word, symbols do not count as separate word a,b,c =>
  • cc: change the whole line
  • s: remove one character and enter INSERT mode
  • {num}s: remove specified number of characters and enter INSERT mode
  • S: remove the whole line, same to cc
  • r: replace a single character
  • R: enter REPLACE mode, replace characters until ESC is pressed.

{num}s vs {num}r

abcdef

  • 3sz<ESC> => zdef: remove 3 characters and enter insert mode, enter z, press ESC to exit INSERT mode
  • 3rz => zzzdef: replace 3 consecutive characters to z

~ switch between uppercase and lowercase

d: Delete

dw: delete word and space after the word de: delete word but preserve space

foo bar:

  • dw => bar
  • de => bar
  • db: delete backward
  • d0: delete to the beginning of the line
  • d$, D: delete to the end of the line
  • dG: delete to the end of the file
  • d?{pattern}: Delete from before the cursor up to the pattern, the pattern is deleted.
  • d/{pattern}: delete from cursor to the pattern, the pattern is preserved.
  • df{char}: deletes up to and including the named character x.

x: delete a single character X: delete the character before the cursor.

xp (delete character and put after cursor) to transpose two letters.

  • "3p copy from 3rd register

This works only for a deleted line. Words, or a portion of a line, are not saved in a buffer. If you want to restore a deleted word or line fragment, and u won’t work, use the p command by itself.

  • <C-h>: Delete back one character (backspace)
  • <C-w>: Delete back one word
  • <C-u>: Delete back to start of line

y: Yank

Y = yy: copy a whole line

  • p: paste after this line
  • P: paste above this line

Yanking uses the same buffer as deleting.