Skip to main content

Mastering Cut, Copy, and Paste in VI Editor: A Complete Guide

Manipulating text is one of the core functionalities of any text editor, and VI is no exception. The VI editor offers efficient and diverse commands to cut, copy, and paste text. This article will walk you through the commands that allow you to perform these operations, coupled with practical examples, to ensure a comprehensive understanding of these essential text-editing operations in VI editor.

Creating a Sample Text File

Before we start, let’s create a sample text file for practicing these commands:

vi practice_text.txt

Once in VI, press i to enter Insert Mode, and type the following text:

This is a practice text file.
We are using this file to demonstrate
the cut, copy, and paste commands
in the VI editor.

Press Esc to exit Insert Mode, and type :wq to save and exit the file.

Cutting, Copying, and Pasting Text in VI Editor

Cutting Text

To cut text in VI, you delete it with the intention to paste it somewhere else. Here are the commands related to cutting text in VI:

  • dd: Deletes (cuts) the line where the cursor is and stores it in the buffer.
  • Nd: Deletes (cuts) the N number of lines starting from the cursor.

Copying Text

To copy text in VI, you use the yank command, which copies the text into a buffer without deleting it. Here are the yank commands:

  • yy: Copies (yanks) the line where the cursor is.
  • Ny: Copies (yanks) the N number of lines starting from the cursor.

Pasting Text

To paste text in VI, you use the put command, which retrieves the text from the buffer. Here are the put commands:

  • p: Pastes the text stored in the buffer below the cursor.
  • P: Pastes the text stored in the buffer above the cursor.

Table: VI Cut, Copy, and Paste Commands and Their Descriptions

CommandDescription
ddCuts the line where the cursor is located.
NdCuts the N number of lines starting from the cursor.
yyCopies the line where the cursor is located.
NyCopies the N number of lines starting from the cursor.
pPastes the text stored in the buffer below the cursor.
PPastes the text stored in the buffer above the cursor.

Practical Examples

Cutting and Pasting Text

  1. Open the file (vi practice_text.txt), place the cursor on any line and press dd to cut the line.
  2. Move the cursor to the location where you want to paste the cut line and press p to paste below the cursor or P to paste above the cursor.

Copying and Pasting Text

  1. Place the cursor on any line and press yy to copy the line.
  2. Move the cursor to the location where you want to paste the copied line and press p to paste below the cursor or P to paste above the cursor.

Conclusion

Understanding and mastering the commands for cutting, copying, and pasting text in the VI editor are crucial for anyone wishing to be proficient in VI. These commands, albeit basic, form the essence of text manipulation, allowing users to move or duplicate text efficiently within a file or between files. Practicing these commands on a sample file will not only make text editing in VI faster but also more precise and enjoyable.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle