To get the most out of Vim (Vi Improved), you must learn how to navigate and handle text efficiently if you want to be as productive as possible when coding and editing text. One of the most important things every Vim user should know how to do is cut, copy, and paste text. So for efficient text editing in Vim/vi you need to get into details of how to undo and redo changes in Vim / Vi.
Although these actions may appear simple, Vim’s modal editing system provides special commands and methods that make them more efficient. You may greatly improve your editing process and advance your expertise with Vim by learning the subtleties of cutting, copying, and pasting in this potent text editor.
In Normal Mode Cut, Copy, and Paste
Ensure you are in regular mode (text editing/command mode) before you begin. Using Esc is the most effective way to do this. You may quickly navigate through the content in this mode.
Vim – Copying
In Vim, yanking is another term for text copying. For this action, use the y key on the keyboard. Many yank commands exist based on how much text you wish to copy. After entering normal mode, position the cursor where necessary and execute the relevant command.
Put the cursor at the beginning of the line and type to copy the complete line:
yy
Place the cursor where you want to start copying and type to copy three (3) lines:
3yy
Move the mouse to the beginning of the word and type the following to copy a word along with its trailing whitespace:
yaw
Move the mouse to the beginning of the word and type the following to copy a word without the trailing white space:
yiw
Use the following command to copy everything from the pointer to the end of the line:
y$
To move the cursor to the beginning of the line and copy everything to its left, type:
y^
Use the following command to copy every character on the line between the cursor and the designated character:
ytx
The order is for “yank till x.” Change x to the character you wish to copy the information to. There won’t be a character x included.
Use the following command to copy everything in the line between the cursor and a certain character, including that character:
yfx
Vim is told to “find x” by this.
Vim – Cutting
In Vim, cutting text is called deleting. To do this operation, use the d key.
The d command makes it simple to clip or remove text when using Vim in normal mode. Here are a few methods for content reduction:
dd
To remove the cursor from the full line, type:
3dd
Starting from the line where the cursor is, use the following to cut three (3) lines:
d$
Vim – Pasting
You can paste text in the desired spot in Vim once you have selected it, regardless of whether you used the yank or delete command. Pasting is referred to as “putting” in Vim, and the p command is used to access the function.
To paste (or put) text, move the cursor to the desired location and press the following keys:
P
The selected text is pasted after the cursor when using this command.
Replace the cursor with the uppercase command to add text before it:
P
In Visual Mode, Copy, Cut, and Paste
As an alternative, you can use the visual selection feature to copy and edit text. You can navigate around to choose text in this mode.
To quit the mode that was previously utilized, press Esc. To enable visual selection, press:
- Enter v (lowercase) to begin choosing specific characters.
- V to pick the entire line in uppercase
- Use Ctrl+v to pick a block.
Once the desired text has been chosen, you can press:
- y to extract (copy) the text
- d to remove (cut off) the text
- To paste the content, use p.
Make sure to save the file before quitting Vim after editing it.
Final Thoughts on How to Cut, Copy, and Paste in Vim / Vi
In conclusion, developing your cutting, copying, and pasting skills in Vim is essential to utilizing all of this flexible text editor’s features. By familiarizing yourself with the different commands and methods covered, you can navigate files, edit text, and increase productivity.
Recall that the secret to mastering Vim is practice and experimentation, so don’t be afraid to try out new techniques and integrate them into your editing process. If you put in the necessary time and effort, you’ll eventually become proficient at using Vim’s editing features with assurance and accuracy.