Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 3.3 - Mastering Linux Text Editors: Vi and Nano

Introduction to Vi

Vi, short for Visual Editor, is not just an editor but a way of life for many Linux administrators and enthusiasts. It operates on a modal concept, which means it has different modes for inserting text, editing text, and running commands.

Understanding Vi Modes

Vi's modal approach to editing can be disorienting at first, but it offers powerful functionality once you become accustomed to it. Let's explore the modes in more depth:

Normal Mode

Upon launching Vi with vi filename, you start in Normal Mode. This mode is not for entering text; instead, you use it to navigate through a file or execute commands to manipulate text. For instance, moving the cursor around in Normal Mode involves using keys like h (left), j (down), k (up), and l (right), instead of arrow keys. Deleting characters, words, or lines; changing text; and yanking (copying) or putting (pasting) text are all actions performed in this mode.

Insert Mode

Insert Mode is where you can actually insert text into the file. You enter Insert Mode from Normal Mode by pressing i (insert before the cursor), I (insert at the beginning of the line), a (append after the cursor), A (append at the end of the line), o (open a new line below the current line), or O (open a new line above the current line). While in Insert Mode, Vi behaves like a normal text editor.

Command-Line Mode

Command-Line Mode is for performing file and buffer operations. You enter this mode by pressing : in Normal Mode. Once in Command-Line Mode, you can execute commands like :w to save your file, :q to quit, :e filename to edit a new file, and many others. You can even access the power of the Unix shell from here with :!command.

Visual Mode

Though not as commonly referenced as the other modes, Visual Mode (accessed by pressing v in Normal Mode) allows for text selection. You can then perform operations on the selected text such as deleting (d), yanking (y), or changing (c). Visual Line (V) and Visual Block (Ctrl + v) modes allow you to select entire lines or blocks of text.

Distinguishing Vi and Vim

Vi is the original text editor in Unix and Linux systems. Vim, which stands for "Vi IMproved," is an extension of Vi with additional features intended to be helpful in editing program source code. Although Vim is essentially an enhanced version of Vi, it includes important features like syntax highlighting, visual selection, multi-level undo, an extensive plugin system, command-line history, and the ability to open multiple windows.

Understanding Vi's basics will serve as a foundation for effectively using Vim, as most Vi commands are applicable in Vim with additional capabilities. Vim is considered more feature-rich and thus a bit more complex, but learning it after Vi can be significantly more approachable.

Detailed Vi Command Cheat Sheet

Action Vi Command Explanation
Open File vi filename Opens filename in Vi. If the file doesn't exist, Vi starts with a new buffer.
Switch to Insert Mode i Switches from Normal Mode to Insert Mode for text editing.
Save (Write Out) :w Saves changes to the current file.
Save and Exit :wq Writes changes to the file and exits Vi.
Exit without Saving :q! Exits Vi and discards any changes made to the buffer.
Cut (Delete) Line dd Deletes the current line and saves it in the buffer (can be pasted later).
Copy (Yank) Line yy Copies the current line into the buffer without deleting it.
Paste Text p Pastes text from the buffer after the current line or cursor position.
Undo Last Action u Reverts the most recent action. Can be repeated to undo multiple actions.
Redo Last Undone Action Ctrl + r Redoes the action that was just undone.
Search Forward for Text /text Searches forward through the document for text.
Find Next Instance n After a search, find the next instance of the search term.
Global Replace :%s/old/new/g Replaces all occurrences of old with new throughout the file.
Go to Specific Line Number :linenumber Jumps to the line number specified. Replace linenumber with the actual number.
Move to Start of File gg Moves the cursor to the first line of the file.
Move to End of File G Moves the cursor to the last line of the file.
Display Current Line Information Ctrl + g Shows the current line number, file information, and status.
Toggle Line Numbers :set number Toggles the display of line numbers on the left side of the window.
Access Help :help Opens the help documentation.

Discovering Nano

Nano is a more straightforward text editor in Linux, favored for its simplicity and ease of use. It operates in a single mode and displays most of the available commands in a list at the bottom of the terminal window, making it very user-friendly.

Features and Functionality

Nano's interface presents a clean layout with the text buffer occupying the majority of the screen. At the bottom, a list of commands and their corresponding keyboard shortcuts are displayed, updating contextually based on the current action.

Nano supports basic text editing functions such as insertion, deletion, cutting, copying, and pasting of text, as well as undoing and redoing actions, searching and replacing text, and going to a specific line number.

One of the notable features of Nano is its search functionality. You can initiate a search by pressing Ctrl + W, then navigate through the results with Alt + W. Nano also supports regular expression patterns in searches, which can be very powerful for complex editing tasks.

Customization is another area where Nano shines. You can customize it by using the .nanorc configuration file. This allows you to set default options, define syntax highlighting for different file types, and adjust the interface to your preferences.

In summary, while Nano may not have the command depth or mode-based editing prowess of Vi, its straightforward design and on-screen help make it accessible, making it a viable and efficient tool for quick edits and for those who prefer to avoid the complexity of Vi.

Detailed Nano Command Cheat Sheet

Action Nano Command Explanation
Open File nano filename Opens filename for editing with Nano. If the file doesn't exist, Nano will create a new one.
Save (Write Out) Ctrl + O Writes the current buffer to file. Will prompt for a filename if it hasn't been named.
Save and Exit Ctrl + X, then Y Exits Nano after saving any changes made to the buffer.
Exit without Saving Ctrl + X, then N Exits Nano and discards any unsaved changes.
Cut (Delete) Text Ctrl + K Cuts the current line and stores it in a cut buffer.
Paste Text Ctrl + U Pastes text from the cut buffer into the current cursor position.
Undo Last Action Alt + U Undoes the last action performed.
Redo Last Undone Action Alt + E Redoes the last action that was undone.
Search for Text Ctrl + W Prompts you to enter text to search for within the document.
Find Next Instance Alt + W Finds the next occurrence of the previously searched text.
Replace Text Ctrl + \ Prompts to replace one string of text with another.
Go to Specific Line Number Ctrl + _ Jumps to the entered line number. Replace linenumber with the actual number.
Move to Start of File Ctrl + Y Scrolls to the previous screen of text.
Move to End of File Ctrl + V Scrolls to the next screen of text.
Display Current Line Information Ctrl + C Displays the current line and column number, file name, and file status.
Toggle White Space Characters Alt + P Toggles the display of white space characters such as tabs and spaces.
Access Help Ctrl + G Displays the help text for Nano.

Conclusion

Whether your choice is Vi for its command efficiency and powerful editing features or Nano for its simplicity and ease of use, mastering these editors can significantly boost your productivity in Linux. As you familiarize yourself with their commands and functionalities, you will find that each has a unique place in the realm of text editing, and proficiency in both will enrich your skill set within the Linux environment.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.