Introduction
Vim is a commonly used open-source text editor installed by default on most Unix distributions. You can use the text editor in two modes, from a command-line interface or as an independent application in a GUI.
While working in Vim, copying, cutting and pasting text are frequently used shortcuts.
In this tutorial learn how to copy, cut and paste in Vi / Vim editor.
Note: If you don’t have Vim on your system, you may want to check out our guides on How to Install Vim on Ubuntu or How to Install Vim on CentOS 7.
Copy, Cut and Paste in Normal Mode
Before you start, make sure you are in normal mode (text editing/command mode). The best way to do so is to press Esc. This mode allows you to move through the text easily.
Copying in Vim
Copying text in Vim is also referred to as yanking. Use the y
key on the keyboard when performing this operation.
There are a number of yank
commands, mainly differing on the amount of text you want to copy.
Once in normal mode, move the cursor to the needed place and use the appropriate command.
- To copy an entire line, place the cursor at the beginning of the line and type:
yy
- To copy three (3) lines, move the cursor from where you want to begin copying and type:
3yy
- To copy a word with its trailing whitespace, copy the cursor at the beginning of the word and type:
yaw
- To copy a word without its trailing white-space, navigate the cursor at the beginning of the word and type:
yiw
- To copy everything right of the cursor to the end of the line, use the command:
y$
- To copy everything left of the cursor to the start of the line, type:
y^
- To copy everything between the cursor and a specified character in the line, use the command:
ytx
The command stands for “yank till x”. Replace x with the character to which you want to copy to. The character x will not be included.
- To copy everything between the cursor and a specified character in the line (including that character), use the command:
yfx
This instructs Vim to “find x”.
Cutting in Vim
Cutting text is referred to as deleting in Vim. Use the d
key when performing this operation.
If you are using Vim in normal mode, you can easily cut or delete text using the d
command. Here are some ways you can cut content:
- To cut the entire line in which the cursor is located type:
dd
- To cut three (3) lines, starting from the one where the cursor is located use:
3dd
- To cut everything right of the cursor to the end of the line use the command:
d$
Pasting in Vim
Once you have selected text in Vim, no matter whether it is using the yank
or the delete
command, you can paste it in the wanted location.
In Vim terminology, pasting is called putting and the function is utilized with the p
command.
You can paste (or put) text by moving the cursor to the wanted position and pressing:
p
Using this command pastes the selected text after the cursor.
To add text before the cursor, type the capitalized command instead:
P
Copy, Cut and Paste in Visual Mode
Alternatively, you can copy and edit text using the visual selection feature. This mode allows you to select text by navigating around.
Use Esc to exit out of the previously used mode and enable visual selection by pressing:
v
(lowercase) to start selecting individual charactersV
(uppercase) to select the entire lineCtrl+v
to select by block
After you have selected the wanted text you can press:
y
to yank (copy) the contentd
to delete (cut) the contentp
to put (paste) the content
Once you have edited in Vim, make sure to save the file before you exit.
Conclusion
Now you know how to copy, cut and paste in Vim. You may often find yourself needing to make configuration changes to your packages. Most of these can quickly be done in Vim using cut & copy paste.
Next, you can explore the many options VIM to offer, such as Vim color schemes.
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/dev/223219.html