Text Editor in Linux

Text Editor in Linux

The premier text editor for Linux and UNIX is a program called vi. While there are numerous editors available for Linux that range from the tiny editor nano to the massive emacs editor, there are several advantages to the vi editor:

  • The vi editor is available on every Linux distribution in the world. This is not true of any other editor.
  • The vi editor can be executed both in a CLI (command line interface) and a GUI.
  • While new features have been added to the vi editor, the core functions have been around for decades. This means that if someone learned the vi editor in the 1970s, they could use a modern version without any problem. While that seems trivial, it may not seem so trivial twenty years from now.

In reality, most Linux systems don’t include the original vi, but an improved version of it known as vim, for vi improved. This fact may be hidden by most Linux distributions. For the most part, vim works just like vi, but has additional features.

To get started using vi, simply type the command followed by the pathname to the file to edit or create:

There are three modes used in vi: command mode, insert mode, and ex mode.

Command Mode Movement

Initially, the program starts in command mode. Command mode is used to type commands, such as those used to move around a document, manipulate text, and access the other two modes. To return to command mode at any time, press the Esc key.

Once some text has been added into a document, to perform actions like moving the cursor, the Esc key needs to be pressed first to return to command mode. This seems like a lot of work, but remember that vi works in a terminal environment where a mouse is useless. Movement commands in vihave two aspects, a motion and an optional number prefix, which indicates how many times to repeat that motion. The general format is as follows:

The following table summarizes the motion keys available:

MotionResult
hLeft one character
jDown one line
kUp one line
lRight one character
wOne word forward
bOne word back
^Beginning of line
$End of the line

These motions can be prefixed with a number to indicate how many times to perform the movement. For example, 5h would move the cursor five characters to the left and 3w would move the cursor three words to the right. To move the cursor to a specific line number, type that line number followed by the G character. For example, to get to the fifth line of the file type 5G. 1G or gg can be used to go to the first line of the file, while a lone G will take you to the last line. To find out which line the cursor is currently on, use CTRL+G.

Command Mode Actions

The standard convention for editing content with word processors is to use copy, cut, and paste. The vi program has none of these. Instead, vi uses the following three commands:

StandardViMeaning
cutddelete
copyyyank
pasteP | pput

The motions learned from the previous page are used to specify where the action is to take place, always beginning with the present cursor location. Either of the following general formats for action commands is acceptable:

Delete

Delete removes the indicated text from the page and saves it into the buffer, the buffer being the equivalent of the “clipboard” used in Windows or Mac OSX. The following table provides some common usage examples:

ActionResult
ddDelete current line
3ddDelete the next three lines
dwDelete the current word
3dwDelete the next three words
d4hDelete four characters to the left

Change

Change is very similar to delete; the text is removed and saved into the buffer, however, the program is switched to insert mode to allow immediate changes to the text. The following table provides some common usage examples:

ActionResult
ccChange current line
cwChange current word
c3wChange the next three words
3dwDelete the next three words
c5hChange five characters to the left

Yank

Yank places content into the buffer without deleting it. The following table provides some common usage examples:

ActionResult
yyYank current line
3yyYank the next three lines
ywYank the current word
y$Yank to the end of the line

Put

Put places the text saved in the buffer either before or after the cursor position. Notice that these are the only two options, put does not use the motions like the previous action commands.

ActionResult
pPut (paste) after cursor
pPut before cursor

Searching in vi

Another standard function that word processors offer is find. Often, people use CTRL+F or look under the edit menu. The vi program uses search. Search is more powerful than find because it supports both literal text patterns and regular expressions. To search forward from the current position of the cursor, use the / to start the search, type a search term, and then press the Enter key to begin the search. The cursor will move to the first match that is found.

To proceed to the next match using the same pattern, press the n key. To go back to a previous match, press the N key. If the end or the beginning of the document is reached, the search will automatically wrap around to the other side of the document. To start searching backwards from the cursor position, start by typing ?, then type the pattern to search for matches and press the Enter key.

Insert Mode

Insert mode is used to add text to the document. There a few ways to enter insert mode from command mode, each differentiated by where the text insertion will begin. The following table covers the most common:

InputPurpose
aEnter insert mode right after the cursor
AEnter insert mode at the end of the line
iEnter insert mode right before the cursor
IEnter insert mode at the beginning of the line
oEnter insert mode on a blank line after the cursor
OEnter insert mode on a blank line before the cursor

Ex Mode

Originally, the vi editor was called the ex editor. The name vi was the abbreviation of the visual command in the ex editor which switched the editor to “visual” mode.
In the original normal mode, the ex editor only allowed users to see and modify one line at a time. In the visual mode, users could see as much of the document that will fit on the screen. Since most users preferred the visual mode to the line editing mode, the ex program file was linked to a vi file, so that users could start ex directly in visual mode when they ran the vi link. Eventually, the actual program file was renamed vi and the ex editor became a link that pointed the vi editor.

When the ex mode of the vi editor is being used, it is possible to view or change settings, as well as carry out file-related commands like opening, saving or aborting changes to a file. In order to get to the ex mode, type a : character in command mode. The following table lists some common actions performed in ex mode:

InputPurpose
:wWrite the current file to the filesystem
:w filenameSave a copy of the current file as filename
:w!Force writing to the current file
:1Go to line number 1 or whatever number is given
:e filenameOpen filename
:qQuit if no changes made to file
:q!Quit without saving changes to file

A quick analysis of the table above reveals that if an exclamation mark, !, is added to a command, it then attempts to force the operation. For example, imagine you make changes to a file in the vi editor and then try to quit with :q, only to discover that the command fails. The vi editor doesn’t want to quit without saving the changes you made to a file, but you can force it to quit with the ex command :q!.

Leave a Reply
Your email address will not be published. *

This site uses Akismet to reduce spam. Learn how your comment data is processed.