How to Copy and Paste in Vim/Nvim with GIF Examples

Copy and paste text is an essential operation for any text editor. As a terminal editor, Vim provides various commands to copy and paste text in its normal mode, visual mode, and insert mode. By utilizing these Vim commands, you can easily copy and paste any text from your documents.

In this blog post, I’ll show you how you can copy and paste text in Normal mode, Visual mode, and Insert mode while preserving your tone and avoiding introducing new words.

Copying Text in Normal Mode (Yanking)

Yanking refers to the process of copying text in the normal mode of the Vim editor. In normal mode, Vim supports copying a line, a word, and even a character.

Copying Lines(y)

The y command is used to copy text in normal mode. To copy a single line, put the cursor on the line and press yy.

yy

If you want to copy multiple lines at once, you can specify the range in the y command. For example, if you want to copy from line 3 to line 7, you can use the y command as follows:

:3,7y

If you don’t know how to show line numbers in Vim, you can use the :set number command to enable line numbers. You can learn more about enabling line numbers in our blog post.

Copying a Word (yw)

You can also copy a single word in Vim using the yw command. To copy a word, place your cursor on the word you want to copy and press yw.

yw

Copying a Character (yl)

In Vim, you can even copy a single character using the yl command. The yl command copies the character under the cursor.

yl

Pasting Text in Normal Mode

In normal mode, you can use the p command to paste your yanked (copied) or deleted text in the Vim editor. You can paste both before and after your cursor position.

Put Command (p):

The p command is used to paste the yanked or deleted text after the cursor position in normal mode. By default, it pastes the text on a new line below the current line.

p (lowercase)

Put Before Command (P):

Vim also allows you to paste your yanked (copied) or deleted text before your current cursor position by using the P (uppercase) command in normal mode. By default, it pastes the text above the current line.

P (uppercase)

Copying Text in Visual Mode

Visual mode offers a flexible option to copy text in Vim editor. In visual mode, you don’t have to switch to normal mode for copying text. Unlike normal mode, you don’t need to specify the range of lines you want to copy. Instead, you can just use navigational keys to select text in your document and copy it.

You can enter visual mode by pressing the v command. Then use the h, j, k, and l keys to navigate and select text in the visual mode in Vim. You will see that as you navigate within your document, the selected text becomes highlighted.

CommandActions
hMove the cursor left
jMove the cursor down
kMove the cursor up
lMove the cursor right

Once you select your desired text in visual mode, press the y command to copy the selected text.

y
Vimcopyvisualmode 1

Pasting Text in Visual Mode

In visual mode, you can paste text over the selected region. This will replace the currently selected text with the newly pasted text.

Pasting in Visual Mode (p):

Use the v command to enter visual mode in Vim. After entering visual mode, use the h, j, k, and l keys to navigate and select text. After selecting the text, use the p command, and it will replace the selected text with the content from the clipboard.

p   (Replace the selected text with the pasted content)

Copying Text in Insert Mode

While insert mode is primarily used for inserting text and editing documents, if you need to copy-paste lines in insert mode, you can do it without switching to normal mode.

Copying by Temporarily Switching to Normal Mode

In insert mode, use Ctrl + O to temporarily switch to a special insert mode that acts like normal mode, where you can use normal mode commands. After pressing Ctrl+O, select the text using your mouse and press the y key to copy the text. Then you can press Ctrl + O again to switch back to your regular insert mode.

Ctrl + O 

Using Terminal Default Settings

Another way to copy text in normal mode is to use the terminal’s default keybinding for copying. For example, in Linux, you can select your text using Ctrl + Shift + C to copy the text.

Ctrl + Shift + C

Please note that the keybinding may be different depending on your operating system.

Pasting Text in Insert Mode

While insert mode is primarily used for entering new text, Vim allows you to paste previously copied text using the clipboard. Let’s explore how you can paste text in insert mode.

By Temporarily Switching to Normal Mode

Press Ctrl+O to temporarily switch to normal mode, then navigate to your desired position and use the p command to paste the content. Again, press Ctrl + O to switch back to your regular insert mode.

Ctrl + O (switching to temporary normal mode)
p (paste)

Using Terminal Default Settings

Another way to paste text in insert mode is to use the terminal’s default keybinding for pasting. For example, in Linux, you can paste your text using Ctrl + Shift + V.

Ctrl + Shift + V

Conclusion

Copying and pasting in the terminal is not that easy, but Vim has made this task simple and fast by providing various commands to effectively copy and paste text. In this blog, I’ve provided basic commands to copy and paste text in the Vim editor. What is your favorite way of copy-pasting text in Vim? Don’t forget to share with your friends and colleagues.

Share your love

Newsletter Updates

Stay updated with our latest guides and tutorials about Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *