1.6 Understanding Editors in Linux

1.6 Understanding Editors in Linux

There are a number of text editors available in Linux such as vi, vim, Nano etc. but we will focus on vi because it’s the most popular one.

Vi editor comes installed by default in most operating systems. Run the “vi” command and specify the file name to open it e.g. — vi index.html

The terminal opens the file and you’ll now be inside the editor. The vi editor has 3 modes of operation, the command mode, insert mode and last line mode. When you open a file in the vi editor, you are by default in the command mode and in this mode, you can issue commands such as to copy and paste lines or delete a line or word or to navigate between lines etc. but you can’t write contents to the file. To write contents to the file, you must switch to the insert mode.

Vi (Visual) is a powerful text editor included in most Linux systems. It has a rich set of features and commands that make it a go-to choice for many developers and system administrators. It’s known for its efficiency and ability to handle complex editing tasks with just a few keystrokes.

To edit or create new files such as .txt, .html or .sh, you need to master an editor. Run any of the following command as per need to open the editor and start writing.

$ sudo vi file.txt
$ sudo vim file.txt
$ sudo vi script.sh
$ sudo vim script.sh

Below is a brief overview of some of the most commonly used Vi commands:

vi editing commands

CommandDescription
iInsert at cursor (goes into insert mode)
aWrite after cursor (goes into insert mode)
AWrite at the end of line (goes into insert mode)
ESCTerminate insert mode
uUndo last change
UUndo all changes to the entire line
oOpen a new line (goes into insert mode)
ddDelete line
3ddDelete 3 lines
DDelete contents of line after the cursor
CDelete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
dwDelete word
4dwDelete 4 words
cwChange word
xDelete character at the cursor
rReplace character
ROverwrite characters from cursor onward
sSubstitute one character under cursor continue to insert
SSubstitute entire line and begin to insert at the beginning of the line
~Change case of individual character

These are just a few of the powerful commands available in Vi. Remember, Vi operates in different modes, mainly command mode and insert mode. Command mode is where you enter all your commands, while insert mode is where you can insert text.

Mastering Vi can take some time, but once you get the hang of it, you'll find it an incredibly powerful tool for your text editing needs.

Did you find this article valuable?

Support Vijay Kumar Singh by becoming a sponsor. Any amount is appreciated!