What is vi Editor

Vi or the Visual Editor is the default text editor that comes with most Linux systems. It is a Terminal-based text editor that users need to learn, essentially when more user-friendly text editors are not available on the system. Some other reasons to use Vi include:

  • Vi is available on almost all operating systems.
  • A smart range of shortcuts that comprise of short keystrokes.
  • You can use Vi as an excellent html editor.
  • The Vi commands are so rich that you hardly need to take your hands off the keyboard.
  • Vi editor creates small size files making it light on your storage.
  • Its free.

In this tutorial, we will learn how to launch and use the Vi editor and work with text files.

Starting Vi Editor

Vi Editor for Linux is a Terminal based text editor so first you will need to open the Terminal window. Once you have opened the Linux Terminal you can:

  • Create a File
  • Work on an already existing file

Creating a File with Vi

In order to create a file in Vi, use the following command:

vi filename

Example: vi textfile.txt

To create a file in a specific location, use the following command:

vi /path/to/filename

Creating a File with Vi

Opening an already existing file

In order to open an already existing file, use the following command:

vi /path/to/filename

Note: A file name by the specified name will be created if does not already exist in the system

The Vi Modes

Before starting to work on files, it is important to learn that the Vi Editor runs in two modes;the Command mode and the Insert mode. In the command mode, you can basically move through text, search for words in a file and save a file etc. You can run various commands but cannot insert anything in your text; for that, you need to be in the Insert mode. Remember that in the Command mode, your typed keys will act only as commands; however in insert mode, you will be able to type and edit the text.

Switching between modes

When you first create or open a file in Vi, you are in Command mode. Although it seems that you can type here, you can’t. To write anything, you need to shift to Insert mode by hitting the i key. In order to shift back to Command mode you simply need to hit the esc key.

Note: It is important to note that the Vi editor is case-sensitive. So if your keys are not giving the desired results, please make sure that you have accidently not hit the Caps Lock key.

Command Mode

Vi usually launches in Command mode. You can perform administrative operations on your files such as saving a file, execute commands on you file, move cursor across the file, cut/yank and paste lines and words in a file. Command mode also enables you to find and replace text in a file.

Moving cursor across a file:

You can usually move the cursor across text in your files with the up, down, left and right arrow keys of your keyboard. In case you are using a remote terminal and the arrow keys fail to behave in the desired manner, you can use the following keys as substitutes:

Move left: h

Move right: l

Move up: k

Move down: j

Insert Mode

To enter text in your file, you need to be in the insert mode. Everything that you type in this mode will be considered text and added to your file.

Working with vi files

Inserting text

After creating a new file or open an already existing file, you can write text in it by first shifting to insert mode by hitting the i key. You will notice that the vi command itself generally does not appear on the screen. You can type a few lines and press <return> at the end of each line. For longer lines, vi does not word wrap the text; it breaks the line at the edge of the screen.

Inserting text with vi editor

Selecting text

You can select a text for copying, cutting and pasting only in command mode. For selecting text, position cursor at the left or right side of the text you want to select and press the v key. You can then move the cursor to select the text.
Selecting text

Copying text

Once you have selected the text, as described above, you can press y to copy the text and x to cut the text. In order to paste the text, position cursor at the desired location and press the p key to paste your copied or cut text. Remember you should be in the command mode to perform these operations.

Deleting text

In order to delete a character from your file, move the cursor until it is on the incorrect letter and then type x key. To remove more characters, say three, type 3x.

Saving and quitting a file

In the command mode type :wq and press enter to write the file to disk and quit vi. The command :w will only write the file to the disk and :q will quit vi without saving the file. You can also split this command for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.

Saving and quitting a file

When you have made changes to your file and quit without saving your changes, vi will give you a warning. You can ignore the warning by using the command :q! And press enter.

Write the file

Common vi Commands

You will be using the following Vi control commands to work around files in Vi editor:

Command Description
Editing a File
i Use this command to insert text before the current cursor location
I Use this command to insert text at the beginning of the line
a Use this command to insert text after the current cursor location
o Use this command to create a new line for text below the current cursor location
Deleting characters
x Use this command to delete the character under the current location
X Use this command to delete the character before the current location
dw Use this command to delete from the current location to the next word
D Use this command to delete from current location till end of the line
dd Use this command to delete the entire line
Copying and pasting
yy Use this command to copy the current line
p Use this command to paste the copied text after the cursor
P Use this command to paste the yanked(cut) text before the cursor
Changing text
cc Use this command to remove contents of the line
s Use this command to replace the character with the character you write
r Use this command to replace the character under the cursor and return to command mode

Conclusion

So this tutorial was all about Vi editor. I hope you have learned to create a new file or open an existing one. You have also learned to insert, delete or remove the words from a text file.

Working With Vi Editor in Linux
Avatar

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.