I believe we can all agree that the copy and paste feature is one of the most wonderful and basic technologies to exist, especially for us developers.
Whether you are working with a basic command, multiple parameters, a simple script, or a massive code block. You are going to come across instances where you need to copy and paste text to and from specific sections.
When it comes to Vim, Vi or Vi-class of editiors, you might be wondering how to copy and paste, especially if you are just starting out.
We wil learn about the vim yaking features, vim registers and more.
Before we get into the command shortcuts on how to copy and paste in Vim, let us talk the basics of registers and yanking in Vim.
In Vim, registers are storage locations used for text manipulation tasks such as yanking (copying), deleting, and pasting.
Each register can hold a text string and be accessed using a specific identifier. Vim features several types of registers, each serving distinct purposes:
- Unnamed Register ("") - The default register. Any yank or delete operation affects this register unless another is specified.
- Named Registers ("a to "z) - These user-defined registers allow for storing and retrieving text explicitly. Each letter corresponds to a different register.
- Read-only Registers ("*, "+, and others) - These registers interface with system clipboard and other read-only data like Vim version or current file name.
- Numbered Registers ("0 to "9) - "0 holds the last yanked text, and "1 to "9 store the last nine deleted or changed texts, with "1 being the most recent.
- Expression Register ("=) - Allows for the execution of Vim expressions and storing their results.
Yanking is analogous to the copy operation in other text editors but is designed to work within Vim's modal editing paradigm and register system.
The primary command for yanking is
y, which can be combined with movement commands to specify the text to be copied.
yy copies the current line, while
y$ yanks from the cursor to the end of the line.
How Yanking Works with Registers
When we perform a yank operation, Vim copies the selected text into a register. By default, this is the unnamed register, but you can specify a named register by prefixing the yank command with
"<register>, such as
"ayy to yank the current line into register
Vim Yank, Delete, and Put
When dealing with Copy/Paste, there are three main terminologies that you need to pay attention to. These include:
- Yank (Y) - refers to the act of copying.
- Delete (D) - this is similar to cutting.
- Put (P) - this refers to the process of pasting.
Vim Copy, Cut, and Paste
The following are the steps you can follow to copy, cut, or paste in Vim/Vi
- Hit the
Esckey to switch back to normal mode in Vim. In this mode, any keystroke you make will be interpreted as a command.
- Move your cursor to the start of the text segment you wish to copy or cut.
- To highlight text for copying or cutting, you can use one of three visual modes:
vto activate visual mode, which allows for character-by-character selection.
Vto activate visual line mode, selecting whole lines at a time.
Ctrl+Vto enter visual block mode, enabling block-wise text selection.
- With visual mode activated, navigate your cursor to the end of the desired text segment. This action highlights the section.
- Execute a copy by pressing
yor perform a cut with
- Position your cursor at the target location for pasting the copied or cut text.
- To paste the text:
Pto paste the text before the cursor's current position.
pto paste the text after the cursor's current position.
The following are the keyboard shortcuts for copy, cut, and paste operations in Vim.
yy- Yanks the entire current line.
5yy- Yanks a specific number of lines, starting from where the cursor is positioned. For instance,
5yywill yank the next five lines, including the one where the cursor currently resides.
y$- Yanks text from the cursor's position to the end of the current line.
y^- Yanks text from the beginning of the line up to the cursor's position.
yiw- Yanks the word under the cursor.
dd- Deletes the entire line where the cursor is, effectively cutting it.
5dd- Deletes a specified number of lines starting from the cursor. Using
5ddwill delete five lines, including the current one.
d$- Deletes text from the cursor's position to the end of the line.
P(uppercase) - Pastes the yanked or deleted content before the cursor's current position.
p(lowercase) - Pastes the yanked or deleted content after the cursor's current position.
In this tutorial, we explored how to perform three basic editing operations in the Vim text editor: copy, cut, and paste. We also learned how vim registers and yanking when you perform the above operations.