DEV Community

Cover image for How To Work With The Vi Editor In Linux
Martins Ngene
Martins Ngene

Posted on

How To Work With The Vi Editor In Linux

Most Unix Based Operating Systems has an editor which it is built with. It is very useful for editing text files, building shell scripts and other things which is required of a text editor.

I am Martins Ngene. I am a Software Engineer, Blogger and Technical Writer. I will give you a little content to help you get started using vi editor.

What is vi editor?

This is the default editor that comes with most of the Unix-based operating systems. The name “vi editor” is the short name for visual editor. In 1976 Bill Joy wrote the first code for the vi editor.

The vi editor is a terminal based editor. It is used to manipulate files directory from the command line. This means you do not have to download it as an application.

How To Use The Vi Editor

To use the vi editor, you need to have a Unix-based operating system installed on your machine. You can use git bash if you are on windows or mac.

To Open A File In Vi:

To open a file in vi use the command:
vi filename

To open a file that was open before your computer crashed use the command:
vi -r filename

Vi Editor

This is what the editor looks like, it shouldn't be too different on your machine.

Modes In Vi Editor

There are two main modes on the vi editor. The Command mode and the insert mode.

Command Mode:
In this mode every character typed is a command which is executed on the text file being edited. When you open the editor it is in the command mode by default.

Command Mode on Vi Editor

Insert Mode:
In this mode, every character entered is added to the text file. To enter this mode click i. Although some commands entered in the command mode can change the editor to insert mode. Use the escape key to leave the insert mode back to the command mode.

Insert Mode on Vi Editor

Moving Around With Your Cursor

To move your cursor around in vi, use the normal arrow keys to move in their respective directions. To move your cursor with letters, use CTRL + k to move upwards, CTRL + j to move downwards. Change the mode to command mode and use “h” to move left and “l” to go right.

Basic Commands In Vi Editor

Before typing any command on the vi editor, you must first type the “:” colon character. I will list some of the basic commands which you can use to edit a text file on your machine:
Inserting Text Into A File:
Just as we discussed earlier, we can use i to enter the insert mode on the vi editor and this enables us type characters into our text files.

Cutting Text In A Text File:
To cut text in the vi editor use the dd command.

Pasting Text Into A File:
To paste some text or a line of text which you cut from another file or document, use the p command.

Undoing An Action:
To undo an action in the vi editor use the u command. This command reverts your action one step backward.

To Move Cursor To The Beginning of Your Current Line:
To move your cursor to the beginning of the current line you are on, use the number 0

To Exit The Editor:
To exit the vi editor, use the q! command.

To Move Cursor To The End of Your Current Line:
To move your cursor to the end of your current line, use the $ command.

To Save An Edited File:
To save an edited file use the w command.

To Save And Exit A File Simultaneously:
To save and exit a file simultaneously, use the wq command.

There you go, a handful of useful commands to get started using the vi editor. You can check out this link: Corolado State University Helpdocs for more commands in the vi editor.

Follow me for more useful content to give you precise knowledge about various technologies, tools and software in general.

Top comments (2)

Collapse
 
knapsack7 profile image
Manoj Verma • Edited

-To insert on new line press 'o' it will change to insert mode and cursor will point to new line.
-To insert after a space press 'a'.
-To replace one character move cursor to that character position and press 'r' followed by your new character.

  • To undo changes press 'u'
  • To remove one character move cursor to that character position and press 'x'
  • To perform forward search press forward slash '/' followed by search string.
  • To perform backward search press '?' followed by search string and press 'n' to move to next match.
Collapse
 
martins_ngene profile image
Martins Ngene

Thanks for the contribution @knapsack7