Copy and Paste in Vim



Linux has a suite of software and tools that Linux users and administrators use to develop or manage the operating system. One of the most commonly used tools is Vim.

Vim is a popular text editor, or even an IDE, that can be used for everything from editing text to developing software to simple tasks like editing a script. It's easy to use, but it requires some time to learn and remember all the commands and how to switch between different aspects and modes.

One of the most essential functionalities for beginners using Vim is copying and pasting. At first, it may seem like a complex operation compared to other editors that use just CTRL+C and CTRL+V. Vim has a different mechanism, which we will demonstrate in this article using examples to make it easy to follow and understand.

Copy and Paste inside Vim

As is known, Vim has two primary modes when dealing with text editing, whether it's normal text or code from a programming language.

To edit text, you use the insert mode, and to apply commands, you use the command mode (or normal mode), which is the mode that appears when you first open Vim.

Let's say, for example, we have a line like this ?

welcome to vim tutorials

To copy text in Vim, you first need to highlight it. To do so, place the cursor on that line and hit ?

V

Here, it's capital V (SHIFT+V). The line will appear highlighted like this ?

To copy the highlighted line, use the command ?

yy

In Vim, "yanking" a text means copying it. For this, we use yy. This means the text is copied. To paste it, use the character p.

Copy Multiple Lines or Words

In the last example, we copied a single highlighted line. However, Vim offers more options when dealing with text. You can copy multiple lines without highlighting them first. For example, suppose we have two lines like this ?

Vim basics by tutorialspoint
How to copy and paste in vim

To copy the two lines, use the following command ?

2yy

The number at the beginning specifies the number of lines you want to copy. To paste them, use the same approach just type p.

Using this method, you can copy multiple lines depending on your needs.

Copy a Word

Vim also has the ability to specify individual words. For example, if you want to copy a word, place the cursor at the beginning of the word and then hit ?

yw

The letter w means "word," so this command copies a word.

If you want to copy an entire paragraph, use the command ?

yip

This will copy the paragraph. To paste it, just hit the letter p.

Copy from Vim to Another Program

Copying and pasting inside Vim is very simple and fast, but sometimes you may want to copy text from Vim to another program, not just within Vim. For this operation, Vim uses something called a register, where the copied text is stored and can be used later.

In simple terms, a register in Vim is a storage area where you can temporarily place text until you need it. These registers can be named, starting from [a-z]. For example, you can copy text and save it in register z, then use it later.

Let's do an example. Suppose we have a text like this ?

tutorials point vim tutorials

To copy this text to a specific register, use the following command in command mode ?

"zyy

The first letter after " is the name of the register that will store the text. To paste this text, use the following command ?

"zp

Here, z is the register name, and p is for pasting the copied text.

Using registers, you can store text in different registers within the same file and use it as needed. Another trick is using numbered registers to paste copied text.

For example, if you want to paste the last text you copied without specifying the register name, you can use the number 0 like this ?

"0p

This will paste the last yanked text. Additionally, you can use numbers from 1 to 9, which store the last nine copied texts.

From Vim to another Window

Now the question is: how can you copy text from one window to another or from Vim itself to another program? By default, Vim does not have access to your system clipboard, so copying from Vim to another program will not work.

To solve this, you can compile a version of Vim from the source that includes clipboard support. However, the simplest solution, if you don't want to compile software, is to use Gvim, the graphical version of Vim. Gvim has access to the system clipboard.

To copy text from Gvim to another program, use the + buffer. For example, if you want to copy this URL to Firefox ?

tutorialspoint.com

Highlight the line you want and use this command:

"+y

When you copy something using the + buffer, it is automatically placed in your system clipboard, and you can paste it like normal text using CTRL+V.

To do the opposite copy text from a browser or another program using CTRL+C you can paste it inside Vim using the command ?

"+p

Conclusion

This was an introduction to how to use copy and paste in Vim. In the first part, we learned how to copy and paste within Vim. In the second part, we explained how to work with Vim and other programs.

Vim is a powerful and interesting editor used by almost all Linux users. It makes development and text editing faster, although it requires some time investment at the beginning to learn it.

Updated on: 2024-12-31T14:56:23+05:30

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements