copy and paste in vi
Categories:
Mastering Copy and Paste in Vi/Vim: A Comprehensive Guide

Unlock the full potential of Vi/Vim's powerful copy, cut, and paste operations. Learn how to efficiently manipulate text using registers, visual mode, and advanced commands.
Vi and Vim are renowned for their efficiency, and a significant part of that efficiency comes from their sophisticated text manipulation capabilities. Unlike typical text editors where copy/paste is a simple Ctrl+C
/Ctrl+V
(or Cmd+C
/Cmd+V
), Vi/Vim offers a more granular and powerful system involving 'yanking' (copying), 'deleting' (cutting), and 'putting' (pasting), all managed through registers. This guide will walk you through the essentials and advanced techniques to master text movement in Vi/Vim.
The Fundamentals: Yank, Delete, and Put
At the core of Vi/Vim's copy-paste functionality are three primary actions: yanking, deleting, and putting. These actions operate on text objects or motions, allowing for precise control over what you copy or cut. The last yanked or deleted text is automatically stored in an unnamed register, ready to be pasted.
yy " Yank (copy) the current line
dd " Delete (cut) the current line
p " Put (paste) after the cursor
P " Put (paste) before the cursor
yw " Yank (copy) a word
diw " Delete (cut) inner word
y$ " Yank (copy) from cursor to end of line
Basic Yank, Delete, and Put Commands
d
(delete) and y
(yank) are operators. They require a motion or text object to act upon. For example, dw
deletes a word, d$
deletes to the end of the line, and diw
deletes the inner word (excluding surrounding whitespace).Leveraging Registers for Advanced Operations
Vi/Vim's registers are a powerful feature that allows you to store multiple pieces of text independently. Think of them as multiple clipboards. There are several types of registers, each serving a specific purpose, but the most commonly used for copy/paste are named registers and the system clipboard register.
flowchart TD A[Start Copy/Cut Operation] --> B{Select Text?} B -- Yes --> C[Enter Visual Mode (v, V, Ctrl+V)] B -- No --> D[Use Operator + Motion (e.g., yy, dw)] C --> E[Yank (y) or Delete (d)] D --> E E --> F{Specify Register?} F -- Yes --> G[Prefix with "a (e.g., "ayy)] F -- No --> H[Uses Unnamed Register] G --> I[Text Stored in Register] H --> I I --> J[Put (p or P) from Register] J --> K[End Paste Operation]
Vi/Vim Copy/Paste Workflow with Registers
Named Registers ("a
through "z
)
These registers are explicitly named and can store text indefinitely until you overwrite them. This is incredibly useful for copying multiple distinct pieces of text and pasting them in different locations or orders.
"ayy " Yank current line into register 'a'
"byw " Yank current word into register 'b'
"ap " Paste content of register 'a' after cursor
"bP " Paste content of register 'b' before cursor
:reg a b " View contents of registers 'a' and 'b'
Using Named Registers
System Clipboard Register ("+
and "*
)
To interact with your operating system's clipboard (allowing you to copy from Vim and paste into another application, or vice-versa), you use the "+
register (for selection clipboard) or "*
register (for primary clipboard, common on X11 systems). This is crucial for interoperability between Vim and other programs.
"+yy " Yank current line to system clipboard
"+p " Paste from system clipboard
"*yy " Yank current line to primary selection (X11)
"*p " Paste from primary selection (X11)
Interacting with the System Clipboard
"+
and "*
registers to work, your Vim installation must be compiled with clipboard support. You can check this by running :version
and looking for +clipboard
or +xterm_clipboard
.Visual Mode for Precise Selection
Visual mode provides an intuitive way to select text before performing an action. It's particularly useful when you need to copy or cut arbitrary blocks of text that don't conform to standard text objects (like words or lines).
1. Enter Visual Mode
Press v
for character-wise selection, V
for line-wise selection, or Ctrl+v
for block-wise selection.
2. Select Text
Use your movement keys (e.g., h
, j
, k
, l
, w
, $
etc.) to highlight the desired text.
3. Perform Action
Once the text is selected, press y
to yank (copy) it or d
to delete (cut) it. The selected text will be placed in the unnamed register (or a specified named register).
4. Paste Text
Move your cursor to the desired location and press p
or P
to paste the content.
v " Enter character-wise visual mode
llll " Select 4 characters
y " Yank selected characters
V " Enter line-wise visual mode
jj " Select 3 lines
d " Delete selected lines
<C-v> " Enter block-wise visual mode (Ctrl+v)
kk " Select 3 lines vertically
ll " Select 3 columns horizontally
y " Yank the rectangular block
Using Visual Modes for Selection
p
will paste it after the cursor, potentially shifting existing text. P
will paste it before the cursor. Experiment to understand the exact behavior.