Shell

Working With Vi Editor in Linux

Working With Vi Editor in Linux

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:

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:

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

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

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.

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.

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.

Common vi Commands

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

CommandDescription
Editing a File
iUse this command to insert text before the current cursor location
IUse this command to insert text at the beginning of the line
aUse this command to insert text after the current cursor location
oUse this command to create a new line for text below the current cursor location
Deleting characters
xUse this command to delete the character under the current location
XUse this command to delete the character before the current location
dwUse this command to delete from the current location to the next word
DUse this command to delete from current location till end of the line
ddUse this command to delete the entire line
Copying and pasting
yyUse this command to copy the current line
pUse this command to paste the copied text after the cursor
PUse this command to paste the yanked(cut) text before the cursor
Changing text
ccUse this command to remove contents of the line
sUse this command to replace the character with the character you write
rUse 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.

How to Change Mouse and Touchpad Settings Using Xinput in Linux
Most Linux distributions ship with “libinput” library by default to handle input events on a system. It can process input events on both Wayland and X...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...
Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...