PW0N's Blog

Sharing more techniques for like-minded people to learn and communicate together.

0%

How to Use Vim Mastering Copy, Paste, and Deletion in Vim

Vim is a powerful text editor used by developers, but mastering its unique commands can be challenging. In this guide, we’ll explore how to efficiently copy, paste, and delete lines in Vim, both within and outside the editor.

How to Copy and Paste from External Sources in Vim

When working with external sources, you might be tempted to use Ctrl+V to paste content into Vim, but Vim handles pasting differently.

Steps to Copy from External Sources:

  1. Copy the text from the external source (e.g., another program or terminal).
  2. Open Vim and switch to command mode by pressing Esc.
  3. Use :+p to paste the copied content into your current file.

If you’re using terminal emulators like Putty or Xshell, you can right-click inside the Vim window to directly paste the content without switching modes.

How to Copy and Paste Within Vim

For copying and pasting within Vim, you can use several built-in commands:

  • To copy a line, simply type yy in normal mode.
  • To paste below the copied line, type p.
  • To paste above, type P (uppercase).

You can also copy specific words or sections of a line:

  • Typing yw copies the word under the cursor.
  • Then, typing p pastes the word in the desired location.

Copying Multiple Lines

To copy multiple lines, use the following steps:

  1. Move the cursor to the first line you want to copy.
  2. Type the number of lines followed by yy. For example, 3yy copies the next three lines.

How to Delete Lines in Vim

Vim’s delete commands are powerful for removing lines or specific sections of text.

  • Delete a line: Type dd in normal mode to delete the current line.
  • To delete multiple lines, specify the range. For example, to delete lines 1 to 3, type :1,3d and press Enter.

Tips for Efficient Line Navigation

Many users overlook the Home and End keys on their keyboards. These keys are particularly useful for quickly jumping to the beginning or end of a line:

  • Press Home to jump to the start of the current line.
  • Press End to jump to the end.

These shortcuts are available on Linux, Windows, macOS, and within Vim itself, making navigation and editing faster.


Keep practicing these Vim commands and you’ll improve your efficiency in no time. As the old saying goes: “Good, good study; day, day up!”