As a command-line text editor vi/vim provides the keys in order to select, copy, cut, and paste the text. For novice users using keys to select, copy, cut, and paste a text or content is very unfamiliar as most of the users use GUI-based text editor which provides cursor and menus for these operations. In this tutorial, we will examine different ways to select, copy, and paste text in vi/vim text editor.
In order to copy or cut we have to select a text area in the vi/vim editor. The current position of the cursor is the start of the select operation. So before starting select move the cursor to the relevant position. Use the following table of keys for a different selection.
|v||Select character by character|
|V||Select the whole line|
|CTRL+v||Select rectangular blocks|
Move the cursor to the end of what you want to copy of cut.
Copy Specified Contet
You have two options after selecting a text or content. You can copy or cut. If you want to copy the content you should use the yy . The y key stands for the yank in vi/vim and used to copy the selected context. You can use the following keys to copy. This operation is also called vim yanking or vim yank.
|yy||Copy the current line|
|y$||Copy from the current location to the end of the current line|
|yiw||Copy current word|
|yaw||Copy current word with leading and trailing spaces|
Cut Specified Content
The second operation is the cut operation which is different from the copy. The cut operation is called delete in vi/vim that executed with the dd keys. The cut operation first deletes selected content and then paste the deleted content to the specified position. You can use the following table in order to cut.
|dd||Cut the current line|
|3dd||Cut 3 lines starting from the current line|
|d$||Cut from current cursor position to the end of file|
|dw||Cut current word|
The copied or cut content can be pasted into the current cursor location by using the p key. The p key will paste the yanked lines into the current location which will complete the copy and paste or cut and paste operation. This is paste key is very similar to the GUI-based text editors or windows right-click paste from clipboard because the content will be pasted from the memory which acts like a clipboard. But keep in mind that this will not paste from the GUI desktop of the Linux operating system. You should use your terminal paste shortcut after changing the Vim/Vi into insert mode. The terminal paste keys are generally CTRL+SHIFT+v .
As a command line and command based text editor Vim or Vi do not provides the right click and paste function. All the paste operation will be done via commands and keys. But GVIM is an exception which is the graphical variant of the Vim and provides some GUI tools and mouse clicks to paste the copied or cut content.
Duplicate Current Line
If you need to duplicate the current line where the cursor is located with simple keystrokes use the following ESC+yy+p keys.
Copy and Paste Specified Line Range
You may need to copy and paste for the specified line range in a fast way just pressing some keystrokes. The line range can be specified from the current cursor location to the down. The ESC+y5y+p keys will copy 5 lines from the current location to down and paste.