A glimpse at the OG File Editor (vi or vim)

Archit KumarArchit Kumar
7 min read

There are multiple file editors in the market like: emacs, nano, gedit (GUI based), but most of the Linux users and even developers prefer vi or vim (Modified Vim) editor.

Things to know in vim editor…

We need to follow certain steps to insert the data in the file with the help of the Vim editor.

  • As soon as we open the file, by using the Vim editor → (vim linux.txt), the file will be opened. Now to enter the data in the file, we need to press i (i.e. go into Insert Mode).
    Note: This will insert the data at the cursor location.

  • To save the file, we have to press → [esc → :w → enter]

  • If we want the file to be saved and quit → [esc → :wq -> enter] or [esc → :x → enter]

  • If we want the file to be saved and quit without using :wq enter → [esc → ZZ]

  • To save, quit, and save and quitforcefully”, we can use ! after the command → [esc → :q! → enter]

If we don’t want to give the file name at first, then we can just open the file editor by running → vim. This will open the Vim editor, and once you type all the data in the file, we can save and quit with the name of the file[:wq linux.txt]

Every command given below runs only in the command mode, if you are in the Insert mode, then first you need to come to the command mode by pressing esc key.

There is also another mode called “Replace mode“, which is used to replace each word of the line with another word.
To toggle from replace to insert mode or insert to replace mode, we can press insert key.

  • To insert the data at the start of the cursor line → I

  • To append the data at the cursor location (or to insert the date after the cursor location) → a

  • To insert the data at the end of the line → A

  • To insert the data in the new line (after the cursor line) → o (Lowercase)

  • To insert the data in the new line (before the cursor line) → O (Uppercase)

  • To copy the single cursor line → yy

  • To copy 5 lines from cursor line → 5yy

  • To copy 5 lines before cursor line + the 6th cursor line → 5y-

  • To copy from the cursor to the start of the line → y^

  • To copy from the cursor to the end of the line → y$

  • To delete the single cursor line → dd

  • To delete 5 lines from the cursor line → 5dd

  • To delete 5 lines before cursor line + the 6th cursor line → 5d-

  • To delete from the cursor to the start of the line → d^

  • To delete from the cursor to the end of the line → d$
    Note: When we delete any line in the vi editor, internally it works as “cut”. We can paste the data in another line.

  • Paste the copied or deleted data, after the cursor location → p (Lowercase)

  • Paste the copied or deleted data, before the cursor location → P (Uppercase)

  • To revert the alteration of data (undo the data) → u

  • To redo everything back again → ctrl + r

  • To go on the top of the file → gg

  • To go to the bottom of the file → G

  • To go to the particular line number (ex: 10), when you are inside the file → 10gg or :10 enter

  • To go to the particular line number (ex: 10), when you are outside the file → vim +10 file.txt

  • To go 10 lines forward from the cursor line10 enter

  • To go 10 lines back from the cursor line10-

  • To search any word in the file from top to bottom (Forward searching) → /word-to-be-searched + enter

  • To search any word in the file from bottom to top (Backward searching) → ?word-to-be-searched + enter

    Note: If we search the word and don’t press enter, it will just show the highlighted text, but,
    If we press enter, it will take us to that line, with permanently highlighted text.
    Now, even if you close the file, and then open the file again, that particular searched word will always be highlighted. To remove that highlighted searched word 👉🏻👉🏻 :nohlsearch

  • To select all the data of the file, in one goggVG

    • gg → Go on the top of the line.

    • V → Start the Visual mode (this will highlight the text).

    • G → Go to the end of the line, selecting all the lines till the end.

Now to copy the data, press → y, and then paste the data in the same file or another file, press → p.

  • To display the line number in the file → :set nu

  • To remove the displayed line numbers from the file → :set nonu

  • To encrypt any file, while creating it with the vim editor → vim -x hello.txtGive the encryption keysave and exit.

  • If we want to encrypt any file, which is already opened → :XGive the encryption keysave and exit

  • To decrypt the file, you have to be inside the file and type→ :set key= entersave and exit

  • If we want anything to be done permanently, (ex: The file should have the line numbers automatically when we open the file with Vim editor → for that → we can save the Vim commands in the file called “.vimrc“, present in the home directory). This will make everything permanent.

    • create the file → vim .vimrc

    • type the command → :set nu or set numbersave and exit

    • Similarly, if we want to disable the permanent search highlighting, we can add the command to the .vimrc file → set nohlsearchsave and exit

  • If we are working on the file, and without exiting the file, we want to check the output of any command:!date → enter.

  • If we are working on the file (ex: hello.txt), and without exiting the file, we want to copy the contents of another file (ex: linux.txt) and paste it into the existing file → :r linux.txt → enter.

  • If we are working on the file (ex: hello.txt), and without exiting the file, we want to add the o/p of any command inside the file → :r !cal → enter.

  • If we want to edit the multiple files with the vim editor in one govim hello.txt linux.txt demo.txt.

    • To know, on which file we are currently present → :args → enter.

    • To move on to the next file → :n → enter.

    • To move back to the previous file → :rew → enter.

What to do, if the file gets crashed in a previous session of editing the file with vim?

Firstly, if the file (ex: /etc/passwd) gets corrupted in the previous shell session while editing with the vim editor, and we got log out from that session.
If we log in back again into the new shell session and try to open the file (vim /etc/passwd), it will show a prompt something like this:

This means that the file (/etc/passwd) is corrupted, and now we need to recover this file.
When the file gets corrupted, it makes a swap file of its name → (/etc/.passwd.swp) which contains unreadable binary data.
The swap file (.passwd.swp) is an internal Vim mechanism for crash recovery.

Steps to recover the main file (/etc/passwd):

  • To recover the main file (/etc/passwd), we can use vim’s built-in recovery feature: vim -r /etc/passwd.

    After running this command, it will give this prompt, just press Enter to go inside the file.

  • Now, manually detect/inspect the file, if the recovered version looks good, save and exit the file with :wq or :x.

  • If you notice corruption or unwanted changes, you can exit without saving with :q!.

    Q. Now, what to do in this case?

    • Mostly all the Linux systems have the backup file of /etc/passwd, which is /etc/passwd-. So, we can restore the /etc/passwd file from its backup file.

    • If there is no backup file available, then we can manually rebuild the file and put the entries in it.

  • If Recovery is Not Needed, then just remove the swap file: rm -rf /etc/.passwd.swp

  • Now, the file (/etc/passwd) is recovered and can be opened easily with the vim editor.

How to learn Vim editor from the command line?

Vim editor’s developers, give us a command line tutor (a guide), to learn about the Vim file editor. If we want to access that guide and learn Vim file editor, just run the command: vimtutor.

1
Subscribe to my newsletter

Read articles from Archit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Archit Kumar
Archit Kumar