Skip to content

Vim - Revisiting Editing

Beyond basic text insertion and movement, Vim offers a rich set of editing features. This section revisits and expands on core editing concepts:

  • Understanding buffers: Vim’s in-memory representation of files
  • Swap files for data recovery
  • Efficiently cutting, copying, and pasting text
  • Undoing and redoing changes

A buffer in Vim is a temporary in-memory area where text from a file is held while you edit it. When you open a file (e.g., vim myfile.txt), Vim reads the file’s contents from the disk into a buffer. All your changes are made to this buffer. The original file on disk is not modified until you explicitly save the buffer (e.g., with :w).

Vim can manage multiple buffers simultaneously, allowing you to work on several files within a single Vim session. You can list open buffers with :ls and switch between them using commands like :bnext (or :bn), :bprevious (or :bp), or :buffer <name/number>.

Vim creates a swap file (usually with a .swp extension) for each buffer you are editing. This swap file periodically stores the unsaved changes from the buffer. Its primary purpose is data recovery: if Vim or your system crashes, you can often recover most of your unsaved work from the swap file when you next try to edit the original file.

Vim will warn you if a swap file already exists for a file you are trying to open, which might indicate an active editing session or a previous crash. You can find the name and location of the current buffer’s swap file with the command:

:swapname

For a file named message.txt, Vim might create a swap file like .message.txt.swp in the same directory, or in a configured central swap directory. These files are automatically deleted when you save and close the buffer normally.

Yanking (Copying), Deleting (Cutting), and Putting (Pasting) Text

Section titled “Yanking (Copying), Deleting (Cutting), and Putting (Pasting) Text”

Vim has powerful commands for yanking (copying), deleting (which also effectively ‘cuts’ text as it’s stored in a register), and putting (pasting) text. These actions typically involve an operator (like y for yank, d for delete) followed by a motion command or a text object.

Common commands (used in Normal mode):

CommandDescription
xDelete character under the cursor (cuts it)
XDelete character before the cursor (cuts it)
dwDelete (cut) from the cursor to the beginning of the next word
dbDelete (cut) from the cursor to the beginning of the current/previous word
DDelete (cut) from the cursor to the end of the line
ddDelete (cut) the entire current line
ywYank (copy) from the cursor to the beginning of the next word
ybYank (copy) from the cursor to the beginning of the current/previous word
Y or yyYank (copy) the entire current line (yy is more explicit; Y often behaves like y$)
pPut (paste) the last deleted or yanked text after the cursor, or after the current line if a whole line was yanked/deleted
PPut (paste) before the cursor, or before the current line

These commands can be prefixed with a count to operate on multiple items. For example:

  • 3dw: Delete (cut) 3 words.
  • 5dd: Delete (cut) 5 lines.
  • 2yy: Yank (copy) 2 lines.

Text manipulated by d, x, X, and y is stored in registers. The default unnamed register is overwritten with each cut or copy. You can use named registers (e.g., "ayy to yank into register ‘a’, "ap to paste from register ‘a’) for more complex operations.

Vim allows you to undo changes you’ve made to the buffer.

  • u: Undo the last change. Pressing u repeatedly will undo changes sequentially.
  • <count>u: Undo the last <count> changes. For example, 5u undoes the last five changes.
  • U (uppercase): Undo all recent changes on the current line, restoring it to the state it was in before you started editing it in the current session of changes on that line. This is different from u, which undoes changes one by one across all lines.

Vim maintains an undo tree, allowing for more complex undo/redo scenarios beyond simple linear history. See :help undo-branches for more details.

Redo reverses the effect of an undo command.

  • Ctrl + r: Redo the last undone change. Pressing it repeatedly redoes subsequent undone changes.
  • :<count>redo: Redo <count> changes. For example, :3redo redoes three undone changes.