How to Copy, Cut and Paste in Vim/Vi

Introduction

Before we begin talking about how to copy, cut and paste in Vim/Vi, let's briefly understand – What is Vim/Vi ?

Vim and Vi are text editors that have been around for several decades and are widely used in the Unix and Linux community. Both Vim ("Vi IMproved") and vi are designed to be lightweight and efficient, offering powerful features for editing text files.

Vim and Vi are primarily used as command-line editors, although they also have graphical interfaces available. They have a steep learning curve but are highly customizable and can be used for a variety of editing tasks, from writing code to editing configuration files or taking notes.

In this tutorial, you will copy, cut and paste in Vim/Vi. We will also address a few FAQs on how to copy, cut and paste in Vim/Vi.

Advantages of Vim/Vi

  1. Efficient editing: Vim/Vi's modal editing, advanced commands, and shortcuts allow for fast and productive editing.
  2. Customizability: Highly customizable with plugins, key mappings, and configuration options to tailor the editor to individual preferences.
  3. Extensive functionality: Offers a wide range of features, including syntax highlighting, macros, search and replace, and split windows.
  4. Portability: Vim/Vi is available on multiple platforms and can be used remotely via SSH, making it ideal for system administration and remote work.
  5. Active community: With a large user base and ongoing development, Vim/Vi benefits from continuous improvement and extensive community support.

Copy, Cut, and Paste in Normal Mode

You're in regular mode when you first open the Vim editor. You can use Vim commands and navigate through the file in this mode.

Simply use the Esc key to return to regular mode from any other mode.

For copying, cutting, and pasting, Vim has its own vocabulary. Copy is referred to as yank (y), cut is referred to as delete (d), and paste is referred to as put (p).

Copying (Yanking)

To copy text, position the cursor where you want it and press the y key, then the movement command. Some useful yanking commands are listed below:

  • yy - The current line is yanked (copied), including the newline character.
  • 3yy - Yank (copy) three lines, beginning with the line where the cursor is currently located.
  • y$ - Yank (copy) the entire line from the cursor to the end.
  • y^ - Yank (copy) from the cursor to the beginning of the line.
  • yw - Copy to the beginning of the next word.
  • yiw - Yank (copy) the current word.
  • y% - Yank (copy) to the corresponding character. (), {}, and [] are the default supported pairs. It is useful to copy text between matching commands.

Cutting (Deleting)

In normal mode, the d key is used to trim (delete) text. Press the d key, then the movement command, after moving the cursor to the desired location. Here are a few deletion commands that will come in handy:

  • dd - Remove the current line, including the newline character, from the file.
  • 3dd - Starting at the line where the cursor is positioned, delete (cut) three lines,
  • d$ - Delete (cut) the entire line from the cursor to the end.

The same movement commands that work for yanking work for deleting. For example, dw deletes everything from the cursor to the beginning of the following word, while d^ deletes everything from the cursor to the beginning of the line.

Pasting (Putting)

Move the cursor to the desired location and press p to insert (paste) the yanked or deleted text after the cursor or P to put (paste) the text before the cursor.

Copy, Cut, and Paste in Visual Mode

You can choose and manipulate text in Vim's visual mode.

  1. Place the cursor on the line where you want to start cutting or copying.
  2. There are three types of visual modes.
  • To access the visual mode, press v.
  • To enter visual line mode, press V. In this mode, the text is picked line by line.
  • Enter visual block mode by pressing Ctrl+v. Rectangle blocks are used to choose the text in this mode.

When you switch to visual mode, you'll be taken to a beginning selection point.

3. To copy or cut text, move the cursor to the end of the text. A movement command or the up, down, right, and left arrow keys can be used.

4. To copy or cut a selection, press y or d.

5. Place your cursor where you wish the data to be pasted.

6. To paste the information before the cursor, press P, and to paste it after the cursor, press p.

FAQs to Copy, Cut and Paste in Vim / Vi

How do I paste text in Vim/Vi? 

To paste text in Vim/Vi, use the "p" command to put the copied or cut text below the current line, or "P" command to put it above. If the text was copy/cut using the yy or dd commands, the put command will paste it after the current line.

Can I copy and paste between different instances of Vim/Vi? 

Yes, Vim/Vi allows you to copy and paste between different instances using the system clipboard. Use the "+y command to copy and "+p to paste.

How can I copy text from an external source to Vim/Vi? 

To copy text from an external source (e.g., another file, terminal, or web browser) and paste it in Vim/Vi, first copy the text from the external source using the regular method (Ctrl+C or right-click and copy), then in Vim/Vi, use the put command ("p") to paste the text.

Is there an undo option for copy, cut, or paste in Vim/Vi? 

No, Vim/Vi doesn't provide an undo option specifically for copy, cut, or paste operations. However, you can use the general undo command ("u") to revert any unintended changes made during these operations.

Can I copy/cut and paste multiple lines in Vim/Vi? 

Yes, Vim/Vi allows you to copy or cut multiple lines. For example, to copy three lines, use "3yy". Paste them using the put command ("p") or put them above the current line with "3P".

How do I copy/cut and paste within a visual selection? 

To copy or cut within a visual selection, first use the visual mode ("v") to select the desired text. Then use the y or d command followed by the motion or text object to copy or cut the selected text. Finally, use the put command ("p") to paste it.

Is there a way to copy/cut and paste without using the command mode? 

Yes, in addition to the command mode, Vim/Vi also supports visual mode for selection-based copy, cut, and paste operations. Use "v" to enter visual mode, select the desired text, and then use the appropriate copy ("y") or cut ("d") command. Finally, use "p" to paste the text.

Conclusion

We've shown you how to copy, cut, and paste with Vim in this tutorial.

If you're new to Vim, go to the Open Vim site to learn how to use it through an interactive tutorial.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.