How To Work With The Vi Editor In Linux
Tags: linux, visualeditor, vi, unixterminal
Published: 2022-03-09 17:58:20

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5da4chmstp6etwywsv3j.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97d36n5z23ztjgmindzy.png)

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 <Esc> escape key to leave the insert mode back to the command mode.


![Insert Mode on Vi Editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rzlddky0c7pzmlp98y9.png)

## **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](https://www.cs.colostate.edu/helpdocs/vi.html) 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.