Learn about Vi Editor

Learn about Vi Editor

7 mins read130 Views Comment
Updated on Feb 14, 2024 16:21 IST

This article revolves around Vi Editor in which different operational modes and controlling commands are covered with its explanation.

2023_02_MicrosoftTeams-image-25.jpg

The VIM editor, an upgraded version of the vi editor, is currently also accessible. VIM stands for Vi IMproved in this context. Because of these reasons, vi is typically regarded as the de facto standard in Unix editors.

  1. It is typically accessible on all types of Unix systems.
  2. All of its implementations are extremely similar.
  3. There are few resources needed.
  4. Compared to other editors like the ed or the ex, it is easier to use.

The vi editor lets you edit already-existing files or start from scratch when making new ones. This editor can also be used just to read a text file.

Table of contents

Starting the Vi Editor

The basic commands for using the vi editor are listed in the following table:

Command Description
vi filename If the file doesn’t already exist, creates a new one; otherwise, open an existing one.
vi -R filename Opens a file that already exists in read-only mode.
view filename Opens a previously opened file in read-only mode.

Opens a previously opened file in read-only mode.

Operation Modes

The following two modes are frequently seen when using the vi editor:

Command Mode: You can carry out administrative actions in this mode, including saving files, running commands, moving the cursor, cutting (yanking), pasting lines or words, and finding and replacing. Anything you type is treated as a command in this mode.

Insert Mode: You can add text to the file using this mode. In this mode, each character that is written is processed as input and added to the file.

Whenever vi launches, it is in command mode. You must be in the insert mode, for which you type I to enter text. Using the Esc key, you can exit the insert mode and return to the command mode.

Hint: Pressing the Esc key twice will switch you to the command mode if you need clarification on which mode you’re in. You use the vi editor to open a file. Start by entering a few characters, then once you’ve done that, switch to command mode to learn the differences.

Getting Out of Vi

q is the command to close vi. Enter the command mode by typing colon, ‘q,’ and then returning. The editor will notify you if your file has been altered in any manner and prevent you from quitting if it has. The command to exit vi without saving is:q!. Ignore this message. By doing so, you can leave vi without storing any modifications.

The editor’s contents can be saved with the w command. You can use:wq and return or combine the command as mentioned above with the quit command.

The ZZ command is the simplest way to leave vi after saving your modifications. Type ZZ to enter the command mode. The ZZ command functions in a similar manner to the wq command.

You can enter the file name after the:w if you want to state or specify a specific name for the file. For instance, you would type:w filename2 and hit the return key to save the file you were working on as a filename called filename2.

Moving Within a File

You must be in command mode to navigate a file without changing the text (press Esc twice). A few commands to move one character at a time are included in the following table:

Command Description
k The cursor is advanced one line.
j The pointer descends one line
h The cursor is shifted left by one character.
l The cursor is positioned one character to the right

The cursor is positioned one character to the right.

To move within a file, the following factors must be taken into account:

1. Case matters in vi. When using the commands, capitalization is important to remember.

2. In vi, you may usually precede a command with the number of times you wish it to happen. As an illustration, 2j shifts the pointer two lines down.

In vi, there are numerous additional ways to navigate a file. Remember that you must be in command mode (press Esc twice). There are a few commands in the following table that can be used to navigate the file:

Top 31 Shell Scripting Interview Questions and Answers [Updated]
Top 31 Shell Scripting Interview Questions and Answers [Updated]
Shell scripting is a computer program to write a sequence of commands for the shell to execute. It helps users learn the command-line better and save time by automating commands...read more
Top Graphic Design Software for 2024
Top Graphic Design Software for 2024
Graphic design software is a category of computer applications and tools that are specifically designed to create, edit, and manipulate visual content for various purposes. These software programs are essential...read more
Top 65 Linux Interview Questions and Answers (2023)
Top 65 Linux Interview Questions and Answers (2023)
This article covers the 65 most asked Linux interview questions that will help you in improving your technical knowledge on Linux.

Control Commands

The Control Key can be used with the following instructions to carry out the tasks listed in the table below:

Editing Files:

You must be in insert mode to edit the file. There are numerous techniques to transition from command mode to insert mode.

Command Description
i Before the current cursor position, the text is inserted.
I Text is inserted at the start of the current line.
an After the current cursor position, the text is inserted.
A Ends the current line with content by incorporating it.
o Creates a new line below the cursor so that text can be entered
O Creates a new line above the cursor so that text can be entered

Deleting Characters:

The key commands that can be used to remove characters and lines from an open file are listed below:

Command Description
x Removes the character that is currently beneath the cursor.
X Deletes the character prior to where the cursor is.
dw Deletes the word after the one under the current cursor position.
d^ Deletes starting at the line’s first character from the current cursor location.
d$ Deletes from the position of the current cursor to the end of the line.
D Deletes up to the end of the line where the cursor is currently located.
dd The line that the cursor is currently on is deleted.

The majority of commands in vi can be preceded by the number of times you wish the action to happen, as was previously described.

Change Commands

In addition, vi allows you to edit characters, words, or lines without erasing them. The relevant commands are listed below:

Command Description
cc Removes the line’s content and returns you to insert mode.
cw Shifts the word that the pointer is now on from the cursor to the word’s final lowercase w.
r Replaces the character that is now highlighted. The replacement is entered, and vi then switches back to the command mode.
R Replaces many characters starting with the one that has the cursor in it. To halt the overwriting, use Esc.
s The character you input will take the place of the existing character. You are then placed in the insert mode.
S Removes the line the cursor is currently on and inserts the new text in its place. Vi stays in insert mode after entering the new text.

Copy and Paste Commands

Using the following instructions, you can copy lines or text from one location and then paste them in another one:

Command Description
yy Copies the current line.
yw Until the end of the word, copies the current word from the character that the lowercase w pointer is on.
p Copies the text and places it after the cursor.
P Bring the grabbed text up in front of the cursor.

Set Commands

You can use the following to alter how your vi screen seems and feels: command sets. Type: set followed by any of the following commands once you are in the command mode.

Command Description
:set ic Ignores the case when conducting a search.
:set ai Autoindent is set.
:set noai Removes auto indent.
:set nu Line numbers are shown on the left side of the display.
:set sw determines a software tabstop’s width. For instance, the command: might be used to provide a shift width of 4. set sw = 4
:set ws If wrapscan is enabled and the word cannot be located at the file’s bottom, it will attempt to find it at the file’s top.
:set wm When this option’s value is larger than zero, the editor will “word wrap” automatically. For instance, you would type the following to set the wrap margin to two characters: set wm = 2
:set ro The file type is changed to “read-only”
:set term Prints terminal type
:set bf Controls characters from the input are discarded.

Running Commands

Vi can execute commands directly from the editor. You merely need to enter command mode and type:! Command to execute a command.

For instance, you can type:! ls to see the result of ls and inspect whether a file exists before attempting to save your file with that filename.

Important Points to be noted

The ideas listed below will help you succeed with Vi.

To use the commands, you must be in command mode. (To ensure that you are always in command mode, press Esc twice.)

The commands must be used with caution. These take cases into account.

To enter text, you must be in insert mode.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio