Learn how to copy 4 lines and paste it to another file in vi editor. Here is how to cut, copy and paste in vi editor. Learn to cut and paste multiple lines in vi editor.
NOTE:
- Vim has its own terms for copy, cut, and paste. Copy is called yank (y), cut is called delete (d), and paste is called put (p).
- All of the following set of commands will only work when you are in command mode. Press the Esc key to enter the command mode – if you are in insert mode.
Yank or Copy
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 – Yank (copy) the current line. It will also include the newline character.
- 3yy – Yank (copy) three lines. It will copy starting from the line where the cursor is present.
- y$ – Yank (copy) everything from the start of the cursor to the end of the line.
- y^ – Yank (copy) everything from the start of the cursor to the start of the line.
- yw – Yank (copy) to the start of the next word.
- yiw – Yank (copy) the current word (cursor positioned).
In simple words, yy yanks copies one line and 5yy yanks copies 5 lines.
Delete or Cut
In the normal mode of vi editor, d is the shortcut key for deleting text (to cut is known as to delete in vi). To cut, simply move the cursor to the desired position and press the d key.
- dd – Delete or cut the current line. It will also include the newline character.
- 3dd – Delete or cut three lines, starting from the line where the cursor is present.
- d$ – Delete or cut everything from the cursor posotion to the end of the line.
In simple words, dd will delete one line and 5dd will delete 5 lines.
Put or Paste
To paste in vi editor, simply move the cursor to the desired location and press p to put or paste. When you press p it will paste the text after the cursor and if you press P it will paste before the cursor.