Development

Vim Copy and Paste

Captain Salem 3 min read

Vim Copy and Paste

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.

In this tutorial, we are going to learn everything you need to know about copying and pasting in the Vim text editor. 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.

Vim Registers

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:

  1. Unnamed Register (““) - The default register. Any yank or delete operation affects this register unless another is specified.
  2. Named Registers (“a to”z) - These user-defined registers allow for storing and retrieving text explicitly. Each letter corresponds to a different register.
  3. Read-only Registers (“*,”+, and others) - These registers interface with system clipboard and other read-only data like Vim version or current file name.
  4. 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.
  5. Expression Register (“=) - Allows for the execution of Vim expressions and storing their results.

Yanking

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.

For instance, 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 a.

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

  1. Hit the Esc key to switch back to normal mode in Vim. In this mode, any keystroke you make will be interpreted as a command.
  2. Move your cursor to the start of the text segment you wish to copy or cut.
  3. To highlight text for copying or cutting, you can use one of three visual modes:
    • Press v to activate visual mode, which allows for character-by-character selection.
    • Press V to activate visual line mode, selecting whole lines at a time.
    • Press Ctrl+V to enter visual block mode, enabling block-wise text selection.
  4. With visual mode activated, navigate your cursor to the end of the desired text segment. This action highlights the section.
  5. Execute a copy by pressing y or perform a cut with d.
  6. Position your cursor at the target location for pasting the copied or cut text.
  7. To paste the text:
    • Press P to paste the text before the cursor’s current position.
    • Press p to paste the text after the cursor’s current position.

Keyboard Shortcuts

The following are the keyboard shortcuts for copy, cut, and paste operations in Vim.

Copy (Yank)

  • yy- Yanks the entire current line.
  • 5yy - Yanks a specific number of lines, starting from where the cursor is positioned. For instance, 5yy will 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.

Cut (Delete)

  • dd - Deletes the entire line where the cursor is, effectively cutting it.
  • 5dd - Deletes a specified number of lines starting from the cursor. Using 5dd will delete five lines, including the current one.
  • d$ - Deletes text from the cursor’s position to the end of the line.

Put (Paste)

  • 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.

Conclusion

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.

:) Cheers!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.