Change content in a file using copying, pasting, and deleting is a regular task for Linux users. You can use Vim and Vi command-line tools for this kind of file manipulation tasks. In this tutorial, You will learn how to use the most common methods of using the vim/vi tool for copy, paste, and delete tasks.
Most Linux distributions offer the Vi tool out of the box. Moreover, you can install vim by running the following command.
$ sudo apt-get install vim -y
$ sudo yum install vim -y
You can open a file using vim as follows.
$ vim file-name
By default, vim opens the file in normal mode. If you want to add content to a file using vim, then you have to press
i to switch into INSERT mode. Press ESC to return to normal mode.
Vim/Vi – Copying (Yanking)
Copying in Vim/Vi tools calls yanking. We can use y for copying in different ways in the normal mode. Let’s see how.
y – Copy the current line. yy – Copy the current line, including the newline character. 5yy – Copy five lines, starting from the line where the cursor is positioned. y^ – Copy everything from the cursor to the start of the line. y$ – Copy content begins from the cursor point to the end of the line. yw – Copy to the start of the next word.
Vim/Vi – Copying in Visual mode
Visual mode allows the user to select text in multiple lines and change the file content.
v – Press to enter visual mode in Vim/Vi.
V (shift +v) – Press to enter visual line mode where the user can select text as entire lines.
Ctrl + v – Press to enter visual block mode. Which means the user can select content as a block.
You can use
y key to copy what you have selected in visual mode.
Vim/Vi – Pasting
After Copying the text in the file you can press
p key for pasting. There are few options available for you.
But, if you were in visual mode, then you have to press
ESC key to return to the normal mode before pasting.
Move the cursor to the desired location where you want to place it. Then press p to paste the copied content after the cursor. Press P(Shift + p) to paste the copied content before the cursor located line.
Vim/Vi – Deleting
You can use
d key to delete the content of a file in normal mode.
dd – Delete the cursor locating line.
5dd – Delete five lines, starting from the cursor locating line,
d$ – Delete everything from the cursor location to the end of the line.
Vim/Vi – Search
/ after opening the file with vim/vi editor in normal mode. Now you can type any word or number you want to search within the file content. press
n key for the jump to the next matching location.
Vim/Vi – Search and Replace
You can search for a text/number and replace it with the following command. This should be done while using normal mode.
In the above command, s means substitute, g means globally substitution, and c for confirming each match before replacing it. Use I to case sensitive matches.
If you wish to provide a range for search and replace command, do as below command. This will search for matching locations only in between 20 to 30 lines.
In this tutorial, we have explained how to copy, paste, and delete in the vim/vi command-line tools. You can refer to Vim/Vi man pages for more details. Please discuss your doubts and suggestions in the comment section below.