Vim Yank and Put


Yanking and putting in Vim are analogous to copying and pasting in other text editors. They provide a powerful and efficient way to move text around within your file or even between different files.

Yanking (Copying)

The y operator is used for yanking (copying) text. It’s often combined with a motion to specify what text to yank. Here are a few examples:

Putting (Pasting)

The p operator puts (pastes) the yanked text after the cursor or the current line. P (uppercase) puts the yanked text before the cursor or the current line.

Examples:

Using Registers

Vim has named registers that allow you to store multiple yanked items. You can specify a register by using "register before the yank or put command.

Example with Registers:

Let’s say you want to swap two words. You can use registers to achieve this:

  1. Position the cursor on the first word.
  2. "ayiw: Yank the first word into register a.
  3. dw: Delete the first word.
  4. Move the cursor to the second word.
  5. "byiw: Yank the second word into register b.
  6. dw: Delete the second word.
  7. "ap: Put the first word (from register a).
  8. "bp: Put the second word (from register b).

By mastering yanking, putting, and registers, you’ll significantly enhance your text editing efficiency in Vim.