How to Edit With Vim in Linux? [Explained]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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

insert text to a file in vim

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:

add a new line to a file in vim

Now, type “Hello World” and press Esc key to get the following output:

add a new line to a file

1.2. Add a Line Above the Current Line

You can also add a line above the current line by pressing SHIFT+o:

add a new line to a file above current line

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:

add line number

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:

move to line 2

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:

  1. First, write :saveas hh.txt after opening the “file.txt” in Vim:

    rename file

  2. Then, press ENTER to save the file contents in the new file:

    rename file2

  3. 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:

move to the beginning of the file line

To go to the beginning of the last line of a file, press SHIFT+g:

move to the beginning of the last line

Consider using $ to move the cursor to the end of a file line:

move to the end of a 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:

Searching a word in a line using vim

The cursor will go to the 1st “Linux” word found in the file after pressing ENTER:

Searching a word in a line using vim2

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:

Searching a word in a backward way

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:

replace word using vim

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:

  1. First, place the cursor at the beginning of the text you want to select:select a line using vim
  2. Then, hit the v button to enter visual mode:enter visual mode
  3. Now, highlight the text or a line by moving the cursor:select a line
  4. To select multiple lines, move the cursor to the end of the desired line with the help of the arrow keys:

    select multiple lines

    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:

  1. To execute a cut/delete operation, move the cursor at the start of a line you want to cut/delete.

  2. After that, press dd to cut/delete the line. Here, I will cut/delete the 2nd line “Welcome to Linuxsimply.com”:

    cut a line

    As you can see the selected line has been cut/deleted.

  3. To cut/delete 3 lines, press 3dd:cut 3lines in vim

    Similarly, you can press Ndd to cut/delete the N no of lines. You can recover the cut/deleted lines by pressing u (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:

  1. 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:

    copy a line

  2. Press p to paste the copied line after the cursor position:copy and paste using vim
  3. Now, to copy N number of lines, press Nyy. In this case, I will press 2yy to copy the first two lines, and then press SHIFT+p to paste them before the cursor position:

    copy and paste multiple lines

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:

save and exit a file using vim

Now, to forcefully exit out of a file without saving modifications, simply write :q! and press ENTER:

exit a file without saving changes

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


<< Go Back to Vim in LinuxLinux Text Editors | Learn Linux Basics

Rate this post
Mitu Akter Mou

Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering & Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio

Leave a Comment