Open In App

How to Copy, Cut and Paste in Vi editor

Last Updated : 09 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Vi editor, a powerful text editor found in most Unix-based systems, is a favourite tool for developers, system administrators, and IT professionals. It’s highly efficient for editing text files in the command line interface (CLI). However, many new users often find it challenging to work with basic operations like copy, cut, and paste in Vi.

This guide provides a step-by-step breakdown of how to copy, cut, and paste text in the Vi editor. Whether you're working on a Linux, macOS, or Windows system using Vi or Vim (an enhanced version of Vi), you can use these methods to perform basic editing tasks.

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

Vi2
Enter the EDIT mode

How to Copy Text in Vi Editor

Copying text in Vi involves entering Visual mode to select text and then using the yank command. Follow these steps to copy text efficiently:

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.

vi3
Cursor

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.

vi4
Enter 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.

vi5
Highlight 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.

CommandDescription
yyYank (copy) the entire line
ywYank (copy) the word
y$Yank (copy) to the end of line

How to Cut in Vi Editor

Cutting text in Vi is similar to copying but uses the delete command. Here’s how you can cut text:

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.

vi6
Move cursor

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.

vi7
Enter 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.

vi8
Highlight

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.

vi9
Cut text


CommandDescription
ddCut (delete) the entire line
dwCut (delete) the word
d$Cut (delete) to the end of line

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.
vi10
paste text in Vi Editor

How to Save and Exit Vi Text Editor

Once you've made your changes, you’ll need to save your work and exit Vi:

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

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

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

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

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

Vi/Vim Keyboard Shortcuts

Below is a list of some essential Vi and Vim keyboard shortcuts for copying, cutting, and pasting text, along with other navigation and text manipulation commands.

ActionCommandDescription
Enter Normal ModeEscReturns to Normal Mode from Insert Mode.
Enter Insert ModeiInserts text before the cursor.
Start Visual ModevStarts Visual Mode for selecting characters.
Start Line-wise Visual ModeVSelects whole lines in Visual Mode.
Move Cursor to Start of Line0Moves the cursor to the beginning of the line.
Move Cursor to End of Line$Moves the cursor to the end of the line.
Move to Next WordwMoves the cursor to the beginning of the next word.
Move to Previous WordbMoves the cursor to the beginning of the previous word.
Move to the Next Paragraph}Moves the cursor forward to the next paragraph.
Move to the Previous Paragraph{Moves the cursor backward to the previous paragraph.
Delete (Cut) a CharacterdDeletes the character under the cursor.
Delete (Cut) a WorddwDeletes the word starting at the cursor.
Delete (Cut) a LineddDeletes the entire line where the cursor is placed.
Delete (Cut) Multiple LinesNdDelete multiple lines (replace N with the number of lines). For example, 5d cuts 5 lines.
Copy (Yank) a CharacterylCopies the character under the cursor.
Copy (Yank) a WordywCopies the word starting from the cursor position.
Copy (Yank) a LineyyCopies the entire line where the cursor is placed.
Copy (Yank) Multiple LinesNyyCopies multiple lines (replace N with the number of lines). For example, 3yy copies 3 lines.
Paste Text After CursorpPastes the copied or cut text after the cursor.
Paste Text Before CursorPPastes the copied or cut text before the cursor.
Undo Last ChangeuUndoes the last action performed.
Redo Last UndoCtrl + rRedoes the previously undone action.
Copy Entire FileggVGSelects the entire file and copies it to the buffer.
Delete Entire FileggdGDeletes the entire file starting from the beginning to the end.
Save the File:wSaves the changes made to the file.
Exit Vi/Vim:qExits Vi/Vim. If there are unsaved changes, it will warn you.
Force Quit without Saving:q!Quits without saving changes.
Save and Quit:wqSaves the file and exits Vi/Vim.
Switch Between Buffers:bnextSwitches to the next buffer (file).
Search for Text/<text>Searches for the given text forward in the file.
Search for Previous Text?<text>Searches for the given text backward in the file.
Find Next MatchnMoves to the next occurrence of the search term.
Find Previous MatchNMoves to the previous occurrence of the search term.

Additional Tips for Using Vi

  • Undo Changes: Press 'u' in Normal mode to undo the last change.
  • Redo Changes: Press 'Ctrl + r' to redo an undone change.
  • Navigate Quickly: Use 'G' to jump to the last line of the file or 'gg' to go to the first line.

Why Use Vi Editor

The Vi editor is a classic, lightweight text editor for users who prefer to work in a terminal environment. It’s efficient for managing system configurations, coding, writing scripts, or even editing small text files directly from the command line.

Some of the key reasons to use Vi include:

  • Speed
  • Minimal resource usage
  • Widespread Availability

Conclusion

Mastering the art of copying, cutting, and pasting in the Vi editor can make you much more efficient when editing text files on Linux, macOS, or Windows. Whether you’re a developer, system administrator, or a casual user, knowing how to leverage these powerful features in Vi can significantly boost your productivity. By following this guide and utilizing Vi’s Visual mode, Normal mode, and simple keystrokes, you’ll be able to perform text manipulation quickly and accurately.


Similar Reads