VI Editor: Your Ultimate Guide to Mastering the Text Editor for UNIX and Linux

The VI text editor is the standard editor that is usually available in all UNIX and Linux operating systems and it behaves the same way in everyone of them. Thus, from system administrators to Security Professionals and other professionals in the computer world who interact with a myriad of systems, especially those of UNIX origin, a basic understanding of VI would go a long way in providing some ease to workflow. An administrator does not get stranded when editing configuration files on hardened systems where new and familiar software such as Nano are not available because of company policies or a security professional leaving a footprint in a compromised system by installing text editing software rather than using the VI editor. Admittedly, the VI Editor can pose a monumental challenge to the uninitiated as the VI editor is so rich in features people often find new layers as scenarios and need occur use cases. This piece, however, has tried to pick the basic and common use cases of text editors and provided a guide to these scenarios. It should hopefully be a handbook for the basics of VI follow up pieces could focus on niche use cases.

Though VI doesn’t have a manual entry and thus “man vi” does not work, it has a help page and thus when in doubt you can try “vi –help”. Vi has a further help page in the command by typing “:help” while in the command mode. VI is used at the command line without a mouse and thus there are two modes: the command mode and the insert mode. The command mode is where the execution of various commands occurs. VI also has an insert mode in which the typing of characters into the file is done. To enter the insert mode, “i” or “I” is used to insert text at the cursor position and beginning of the line, respectively. The tables below try to group related commands and functions in the VI text editor.

Creating and opening files with the VI editor

Terminal CommandExplanation
vi [option] [filename]This command creates and opens a file. Please note that vi editor defaults to the command mode. Both [option] and [filename] are optional parameters and “vi” would open the editor and request a file to ‘dump’ created content when you try to exit. Please note that configuration files need to be opened with root privileges if you intend to change them and commands like “sudo” and its equivalents should precede “vi”.
Ex: vi, vi testtext.txt, and vi -n texttext.txt are all acceptable.

The [option] part of the command are optional parameters that affect the behaviour of your vi instance. Optional parameters can only be used when a filename is specified. This list is not exhaustive. More are available using the help command, but this piece covers most use cases.

[option]Explanation
+Start at the end of the file. Vi defaults to the beginning of a file
Ex: vi + testtext.txt
+nPlace the cursor at the specified line number n. It is used when the line to be edited is known beforehand and you want to go there easily (remember no mouse control ideally) Ex: vi +10 testtext.txt takes you to the 10th line in testtext.
+/patternPlace the cursor at the first instance of the pattern.
Ex: vi +/pizza testtext.txt takes you to the first instance of the letter combination ‘pizza’ in the file testtext.
-LList swap files. This is used to view the auto save versions of the file. Useful in forensic analysis to check file history.
Ex: vi -L
-mPrevents writing or more colloquially saving files. Useful for forensic and cybersecurity experts. It ensures the muscle memory actions of writing/saving files while exiting is prohibited.
Ex: vi -m testtext.txt ensures that I don’t mistakenly save modifications to the file.
-nNo swap file, use memory only. With an unexpected shutdown while editing, the file cannot be recovered. It would not show up using -L or -r options. A useful option for a penetration tester, in the event such a tester is shut down, the intention cannot be figured out by checking the VI activities of the tester.
Ex: vi -n testtext.txt
-o[n]Open n number of windows. If filenames are supplied, it assigns the files to each window. This opens in a horizontal stack
Ex: vi -o2
-O[n]This opens the windows in a vertical stack.
Ex: vi -O2
-p[n]Open n tab pages. If filenames are supplied, it assigns the files to each tab.
Ex: vi -p3 testtext.txt, testtext2.txt
-rRecover the edit states of the file using the swap files. This option would only produce results if the “-L” option is not present.
Ex: vi -r testtext.txt.
-RRead-Only mode. You cannot edit files.
Ex: vi -R testtext.txt

Those are some of the most common options that are useful while opening a file in VI. These options and their functions should not cause confusion in tandem with other options that exist after the file is open. First, we look at shortcuts that affect the behaviour of the application on a file. All commands must be entered in command mode, which is done by pressing the escape key (multiple pressing doesn’t have an adverse effect, so use it).

Keyboard ShortcutExplanation
:wqSave the current file and exit the application. Where multiple tabs or windows exist, the commands would affect the currently active window.
:qClose and exit. If unsaved changes exist, the command would not work. It would ask you to either type “!” to continue exiting without saving or press enter to return to insert mode.
‘:x’Save the current file only if changes occur and exit. The difference between “:wq” is that this doesn’t save in the absence of changes.
:q!forcefully close, saving no changes made to the file.
:wSave the current file but keep it open
:w [filename]Save the specified filename
:nEdit the next file when more than one file is open. This command is used to switch between windows or tabs. (Remember, no mouse navigation)
:qallClose all buffers and exit VI. Used to close multiple tabs or windows at once.
:e [filename]Open a specific file within the current directory or a file path for a different directory
:0Move the cursor to the first line in a file
:nMove the cursor to the nth line in a file
:$Move the cursor to the last line in a file
:.=Returns the current line number
:=Counts the number of lines in the file
/lettersSearch forward / downward for the occurrence of a string in text. Here we are searching for the occurrence of “letters”.
?beforeSearch backward / upward for the occurrence of a string of text. Here we are searching for the occurrence of “before”.
nMove to the next occurrence of a searched string toward the search
NMove to the next occurrence of a searched string in the opposite direction of the search.
~Change the case of the selected character.

Other commands exist, especially those that allow you to manipulate text directly, but this is an introductory piece into the world of VI.

One thought on “VI Editor: Your Ultimate Guide to Mastering the Text Editor for UNIX and Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

RELATED

Secure Families: Fight Cyber Kidnapping & AI Risks

Guard your loved ones from digital dangers: Essential strategies to navigate Cyber Kidnapping & AI threats with wisdom and care.

Forensic Toolkit (FTK) – A Digital Forensics Deep Dive

Explore FTK's capabilities in digital forensics: from evidence processing to advanced analysis. Unveil the power of AccessData Forensic ToolKit.

Installing Suricata on Parrot OS: Don’t make the same mistake I did, here’s the simple solution!

The ultimate solution to installing Suricata on Parrot OS: Don't waste time like I did. Follow these easy steps and…

Everything You Wanted to Know About Attack Vectors

An attack vector is the pathway used by an attacker to gain access to the entity being attacked. Often in…