[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 13, 2023

How to Copy, Cut and Paste in Vim / Vi

How To Copy, Cut And Paste In Vim / Vi

This article shows how to copy, cut, and paste in Vim / Vi editor.

When working with text files, copying, cutting, and pasting text is one of the most commonly performed tasks.

Vim or its precursor Vi comes preinstalled on macOS and almost all Linux distributions. Knowing the basics of Vim is helpful in a situation where your favorite editor is not available.

 

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.

Vim has its own terminology for copying, cutting, and pasting. Copy is called yank (y), cut is called delete (d), and paste is called put (p).

 

Copying (Yanking)

To copy text, place the cursor in the desired location and press the y key followed by the movement command. Below are some helpful yanking commands:

  • yy – Yank (copy) the current line, including the newline character.

  • 3yy – Yank (copy) three lines, starting from the line where the cursor is positioned.

  • y$ – Yank (copy) everything from the cursor to the end of the line.

  • y^ – Yank (copy) everything from the cursor to the start of the line.

  • yw – Yank (copy) to the start of the next word.

  • yiw – Yank (copy) the current word.

  • y% – Yank (copy) to the matching character. By default supported pairs are (), {}, and []. Useful to copy text between matching brackets.

 

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.

 

Conclusion

In this guide, we have shown you how to copy, cut, and paste in Vim.

Keep reading
How to use whatis command in Linux.
September 13, 2023

How to use whatis command in Linux.

How To Use Whatis Command. The whatis command is used to get brief information about Linux commands or functions. It displays the manual page description in a single line of the command that passes with the whatis command. It searches for the strings that have been passed with it from its index…

Read article
How to use shred command in Linux.
September 13, 2023

How to use shred command in Linux.

How To Use Shred Command. Deleting a file in Linux or any other operating system does not actually remove the file from the hard drive. The operating system deletes the pointers to the file and marks the occupied space as ready to be written to, while the actual data remains. A deleted file can be…

Read article
How to use passwd command in Linux.
September 13, 2023

How to use passwd command in Linux.

How To Use Passwd Command. On Unix-like operating systems, the passwd command is used to change the password of a user account. A normal user can run passwd to change their password, and a system administrator (the superuser) can use passwd to change another user’s password, or define how that…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00