It is common action to do copy, cut and paste text when working with the text files. Vim is a text editor which is comes preinstalled on macOS and almost all Linux distributions. It’s good to have basics of Vim if your favorite editor is not available. In this article we will show you how to copy, cut, and paste in Vim / Vi editor.
Copy, Cut and Paste in Normal Mode
By default, your are in normal mode when you launch the Vim editor. In this mode, you can run Vim commands and navigate through the file.
Just press Esc
key to switch back to normal mode from any other mode. Vim has its own terms for copying, cutting, and pasting. Copy is called yank (y
), cut is called delete (d
), and paste is called put (p
). Let’s see each in brief:
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
– Copy the current line, including the newline character.3yy
– To copy three lines, starting from the line where the cursor is positioned.y$
– You can copy everything from the cursor to the end of the line.y^
– Copy everything from the cursor to the start of the line.yw
– Yank (copy) to the start of the next word.yiw
– Copy the current word.y%
– To copy to the matching character. By default supported pairs are()
,{}
, and[]
.
Cutting (Deleting)
To cutting the text you should use d
key. Place the cursor at the desired position and press d
key. Below are some helpful deleting commands:
dd
– It will cut or delete the current line, including the newline character.3dd
– To delete or cut three lines, starting from the line where the cursor is positioned.d$
– Delete or 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)
Move the cursor at the desired place to paste the yanked or deleted text and press p
to put the text after the cursor or P
to paste before the cursor.
Copy, Cut and Paste in Visual Mode
Vim’s visual mode allows you to select and manipulate text.
1. Move the the cursor on the line you want to begin copping or cutting.
2. There are three subtypes in the visual mode:
- Press
v
to enter the visual mode - To enter visual line mode press
V
, 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.
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. To copy press y
, or d
to cut the selection.
5. Place 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 article explained how to copy, cut, and paste in Vim text editor. To know more about Vim, visit the Open Vim site.
If you have any questions or feedback, you can leave a comment below.