To edit (insert, copy, paste, delete, navigate, search, replace, select, etc.) a text file using a Vim text editor in Linux, first, open a file with vim filename
command and then edit using commands such as i
to insert text, v
to enter visual mode, dd
to delete line, yy
to copy line, p
to paste line, etc.
In this article, I will explain how to perform various editing operations using Vim. Keep reading!
How to Enter Vim Editing Modes to Edit a File?
To enter different Vim editing modes, go through the below commands:
- Normal Mode: Default mode. If you are not in this mode, press the
Esc
key to enter normal mode from other modes. - Insert Mode: Press
i
to enter insert mode. - Visual Mode: Press
v
to enter visual mode. - Command-line Mode: Type
:
to enter command-line mode. - Replace Mode: Press
R
to enter replace mode. - Select Mode: Press
S
to enter select mode.
How to Edit a File in Vim?
To edit a file with Vim in Linux, create (or open an existing one) a file by writing the vim file.txt
command in the terminal. Then, perform the following editing operations:
1. Insert Text in File
To insert a text in a file, press i
to enter insert mode from the normal mode and start typing the following lines:
Hello Linux User.
Welcome to Linuxsimply.com
Simply press Esc
to go back to normal mode to continue further editing.
1.1. Add a Line Below the Current Line
To insert a new line to the file contents while using Vim, hit the o
button from normal mode to enter insert mode and add a new line at the bottom of the current line:
Now, type “Hello World” and press Esc
key to get the following output:
1.2. Add a Line Above the Current Line
You can also add a line above the current line by pressing SHIFT+o
:
Note: To add a line at the end of the current line, press SHIFT+a
.
2. Show Line Numbers
To show the line numbers in the file, go to the command mode from the normal mode by pressing colon (:)
and then write set number
or set nu
after the colon:
As you can see the line number has been added before each line of the file.
Now, to move to the line number 2, you can write 2 after the colon:
3. Rename File
To rename a file using Vim, first, open a file you want to rename. After that, write :saveas new_filename
which will make a copy of the old file with a new filename. To remove the old file, execute rm old_filename
in the terminal. Here’s a practical example:
-
First, write
:saveas hh.txt
after opening the “file.txt” in Vim: -
Then, press ENTER to save the file contents in the new file:
-
Now, to delete the old file, run the following command in the terminal:
rm file.txt
4. Moving Through File
Though there are arrow keys to navigate through a file, Vim offers different navigation commands. Like to move to the beginning of the first line, you can use gg
command:
To go to the beginning of the last line of a file, press SHIFT+g
:
Consider using $
to move the cursor to the end of a file line:
Note: Here are some other navigation keys:
h
: Shift the cursor to the left side.l
: Shift the cursor to the right side.j
: Moves the cursor down.k
: Moves the cursor up.
5. Search in Vim
Vim can be used to search for a specific pattern in a file. For that, you can use /pattern
syntax. In this example, I am going to find the “Linux” word in the “hh.txt” file by using the /Linux
command:
The cursor will go to the 1st “Linux” word found in the file after pressing ENTER:
Press n
to go to the next occurrence and N
to go to the previous occurrence of the searched terms.
To search backward, type ?Linux
instead of /Linux
:
5.1. Replace Text
After finding the word “Linux”, I will replace it with another word “Ubuntu” using :%s/Linux/Ubuntu/g
. Here’s how:
The picture shows that All “Linux” words in the file have been replaced with “Ubuntu”.
6. Select Line in Vim
To select text or a line in Ubuntu using Vim, follow the step-by-step guide:
- First, place the cursor at the beginning of the text you want to select:
- Then, hit the
v
button to enter visual mode: - Now, highlight the text or a line by moving the cursor:
-
To select multiple lines, move the cursor to the end of the desired line with the help of the arrow keys:
After selecting the desired lines, you can perform various operations like copying, pasting, cutting, deleting, etc.
7. Cut or Delete Line
You can use the dd
command in Vim to cut or delete a line. In Vim, the delete operation is identical to the cut operation because the deleted line will be saved in the register. Here is the full guide on how to do this in Vim:
-
To execute a cut/delete operation, move the cursor at the start of a line you want to cut/delete.
-
After that, press
dd
to cut/delete the line. Here, I will cut/delete the 2nd line “Welcome to Linuxsimply.com”:As you can see the selected line has been cut/deleted.
- To cut/delete 3 lines, press
3dd
:Similarly, you can press
Ndd
to cut/delete the N no of lines. You can recover the cut/deleted lines by pressingu
(undo).
8. Copy and Paste Line
Before utilizing the Vim text editor, one must know the fundamental commands for copying and pasting lines. I’ll walk you through these simple actions in this section:
-
If you want to copy a line in Vim, place the cursor at the beginning of the line you want to copy. Afterward, press
yy
to copy the entire line: - Press
p
to paste the copied line after the cursor position: -
Now, to copy N number of lines, press
Nyy
. In this case, I will press2yy
to copy the first two lines, and then pressSHIFT+p
to paste them before the cursor position:
9. Save and Exit
To save and exit a file, make sure you are in the normal mode. After that, type :wq
or :x
and press ENTER:
Now, to forcefully exit out of a file without saving modifications, simply write :q!
and press ENTER:
Conclusion
To sum up, I have discussed how to edit a file by copying, pasting, selecting, deleting, displaying line numbers, inserting text, etc. while using Vim. After reading this article you will have a great overview of Vim to edit your file. Don’t forget to leave a comment if you encounter any issues while editing your file!
People Also Ask
How do I edit a file in Vim?
To edit a file in Vim, first, open the file with Vim, then switch to insert mode by pressing the i
key and insert text. After that, run some navigation commands to move through the file contents and edit them using different Vim commands like dd
to delete, yy
to copy, p
to paste, v
to enter visual mode, etc. You can also use arrow keys for navigation.
How do I edit a specific line in Vim?
To edit a specific line in Vim, go to the line using arrow keys or commands, then perform editing. For instance, to edit line number 50 in Vim, type :50
from the normal mode. This command will take you to the beginning of the line number 50. Now, you can delete the line by pressing dd
. Additionally, you can copy the line utilizing the yy
command.
How do I paste text in Vim?
To paste text in Vim, first press the v
button to enter visual mode. After that move cursor to select the text and press yy
to copy it to the clipboard. Finally, press p
to paste the text after the cursor position. To paste text before the cursor position, you can press SHIFT+p
.
How do I add a new line in Vim?
To add a new line in Vim, press i
to go to the insert mode. After that, move the cursor to the position where you want to add a line and start typing the line.
How to merge two lines in Vim?
To merge two lines in Vim, place the cursor on the first line you want to merge and press J
. It will merge the current line with the below line.
Related Articles
- How to Install Vim in Ubuntu? [5 Methods]
- How to Install and Use Vim-Plug in Linux [Explained]
- How to Set Vim Color Scheme in Linux? [4 Steps]
<< Go Back to Vim in Linux | Linux Text Editors | Learn Linux Basics
FUNDAMENTALS A Complete Guide for Beginners