Open In App

How to Copy, Cut and Paste in Vi editor

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Vi is a really strong text editor that you can use on Unix and Linux computers. It can do a lot of things, like copying, cutting, and pasting text. Even though these actions might seem hard at first, once you get the hang of them, they can make you much faster when you’re working with text files. In this guide, we’ll talk about how to copy, cut, and paste text in Vi, and we’ll explain it clearly with lots of examples.

Entering Edit Mode in Vi Editor

Before we can copy, cut, or paste text in Vi, we need to enter edit mode. Open your desired file in Vi by typing:

vi filename
Entering vi editor

Entering vi editor

Now, to enter edit mode, press the ‘i’ key. You’ll notice the status line at the bottom changes to “– INSERT –“, indicating that you are now in edit mode.

entering editing mode in vi editor

entering editing mode in vi editor

How to Copy Text in Vi Editor?

Below are the Copy Command for Vi represented in tabular format:

1. Move the Cursor:

When you open a file in Vi, you’ll see a cursor, which is usually a blinking vertical line or block. This cursor indicates your current position in the file. To copy text, you need to move the cursor to the beginning of the text you want to copy. You can move the cursor using the arrow keys on your keyboard. Press the arrow keys until the cursor is positioned at the start of the text you want to copy.

cursor in the beginning

cursor in the beginning

2. Enter Visual Mode:

Once the cursor is in the right place, you need to enter “visual mode”. Think of visual mode as a way to highlight or select text. To enter visual mode, simply press the ‘v’ key on your keyboard. When you do this, you’ll notice that the text changes color or gets highlighted. This indicates that you’re now in visual mode and can start selecting text.

Entering Visual Mode

Entering Visual Mode

3. Highlight the Text:

With the cursor in visual mode, you can now use the arrow keys to highlight the desired text. Move the cursor using the arrow keys while holding down the ‘Shift’ key. As you move the cursor, you’ll see that the text gets selected or highlighted. Keep moving the cursor until you’ve selected all the text you want to copy.

Highlight the text

Highlight the text

4. Copy the Text:

Once you’ve highlighted the text you want to copy, it’s time to actually copy it. To do this, simply press the ‘y’ key on your keyboard. ‘y’ stands for “yank”, which is another way of saying “copy” in Vi. When you press ‘y’, Vi will copy the selected text into its internal clipboard, ready to be pasted elsewhere.

How to Cut in Vi Editor?

1. Move the Cursor:

As with copying text, cutting text in Vi starts with positioning the cursor at the beginning of the text you want to cut. You can navigate through the text using the arrow keys on your keyboard or by using other navigation commands like ‘h’ (left), ‘j’ (down), ‘k’ (up), and ‘l’ (right). Move the cursor until it’s positioned at the start of the text you want to cut.

2024-02-07_16-08

2. Enter Visual Mode:

After placing the cursor at the starting point of the text you wish to cut, you need to enter visual mode. Visual mode allows you to select text visually, making it easier to determine the exact portion you want to cut. To enter visual mode, simply press the ‘v’ key on your keyboard. This action changes the mode to visual, and you’ll notice that the text is highlighted or selected.

Enter in Visual Mode

Enter in Visual Mode

3. Highlight the Text:

With the cursor in visual mode, you can now use the arrow keys to highlight or select the desired text. As you move the cursor using the arrow keys, the text will be highlighted accordingly. You can highlight text character by character, word by word, or line by line, depending on your preference and the text you wish to cut.

Highlight Text

Highlight Text

4. Cut the Text:

Once you’ve selected the text you want to cut, it’s time to actually cut it from the file. In Vi, cutting text is done by pressing the ‘d’ key after selecting the text. ‘d’ stands for “delete” in Vi, which is another way of saying “cut”. When you press ‘d’, Vi will remove the selected text from the file and place it into its internal clipboard.

Cut the Text

Cut the Text

How to Paste Text in Vi Editor?

Pasting text in Vi is straightforward. After copying or cutting text, move the cursor to the desired location where you want to paste the text, then press ‘p’. This will paste the text after the cursor.

Example:

Let’s say we’ve copied or cut the word “sample” from our previous example. To paste it:

  1. Move the cursor to the end of the line or to any desired location.
  2. Press ‘p’ to paste the text.

paste text in vi editor

paste text in vi editor

How to Save and Exit Vi Text Editor ?

Enter Command Mode:

If you’re currently in edit mode (INSERT or REPLACE mode), press the ‘Esc’ key to switch to command mode. You’ll see “– NORMAL –” displayed at the bottom left corner, indicating that you’re in command mode.

Save Changes and Exit:

To save your changes and exit Vi simultaneously, you can use the command ‘:wq’. Type ‘:wq’ (without the quotes) and then press the ‘Enter’ key. This command tells Vi to write (save) the changes to the file and then quit the editor.

Save Changes:

If you want to save your changes without exiting Vi, use the command ‘:w’. Type ‘:w’ and press ‘Enter’. This command instructs Vi to write (save) the changes to the file, but it doesn’t exit the editor. You can continue editing the file after saving.

Exit Without Saving:

If you made changes but you don’t want to save them, you can exit Vi without saving. Use the command ‘:q!’ to force quit without saving. Type ‘:q!’ and press ‘Enter’. Vi will exit immediately, discarding any unsaved changes.

Save and Exit with Confirmation:

If you’re unsure whether changes have been made or if there are unsaved changes, you can use the command ‘:x’ or ‘:wq!’ to save and exit. Type ‘:x’ or ‘:wq!’ and press ‘Enter’. If there are unsaved changes, Vi will prompt you to save before exiting.

Remember, Vi doesn’t have a traditional user interface, so all commands are entered via the keyboard. Once you become familiar with these commands, saving and exiting Vi will become second nature.

Conclusion

This article introduced Vim, a powerful text editor. It covered fundamental techniques such as copying, cutting, and pasting in Vim, both in Visual and Normal modes. Additionally, essential Vim keyboard shortcuts were highlighted, offering users powerful tools for efficient text manipulation and editing. Vi’s text manipulation commands for copying, cutting, and pasting offer powerful capabilities once mastered, enhancing text editing efficiency for proficient users.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads