vi copy line

Vi and Vim editor cut, copy and paste

Copy, Cut and Paste in Normal Mode

When you launch the Vim editor, you’re in the normal mode. In this mode, you can run Vim commands and navigate through the file.

To go back to normal mode from any other mode, just press the Esc key.

Cutting (Deleting)

In normal mode, d is the key for cutting (deleting) text. Move the cursor to the desired position and press the d key, followed by the movement command. Here are some helpful deleting commands:

  • dd – Delete (cut) the current line, including the newline character.
  • 3dd – Delete (cut) three lines, starting from the line where the cursor is positioned,
  • d$ – Delete (cut) everything from the cursor to the end of the line.

The movement commands that apply for yanking are also valid for deleting. For example dw, deletes to the start of the next word, and d^ deletes everything from the cursor to the start of the line.

Pasting (Putting)

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

Copy, Cut, and Paste in Visual Mode

Vim’s visual mode allows you to select and manipulate text

  1. Place the cursor on the line you want to begin copping or cutting.
  2. The visual mode has three subtypes.
    • Press v to enter the visual mode.
    • Press V to enter visual line mode, where the text is selected by line.
    • Press Ctrl+v to enter visual block mode. In this mode, the text is selected by rectangle blocks.Entering the visual mode also marks a starting selection point.

3. Move the cursor to the end of the text you want to copy or cut. You can use a movement command or up, down, right, and left arrow keys.

4. Press y to copy, or d to cut the selection.

5. Move the cursor to the location where you want to paste the contents.

6. Press P to paste the contents before the cursor, or p to paste it after the cursor.

Leave a Comment