Learning how to edit in vi editor is an essential skill for developers, system administrators, and anyone who spends time working in a Linux or Unix terminal. Unlike modern graphical text editors, vi operates entirely through keyboard commands, which can feel intimidating at first glance. Still, once you understand its modal editing system and core shortcuts, vi becomes a lightning-fast tool for modifying configuration files, writing scripts, and managing remote servers. This guide walks you through everything from launching the editor to saving your changes, with practical examples designed for beginners and experienced users alike Worth keeping that in mind. That alone is useful..
What Is the Vi Editor?
The vi editor, short for visual interface, is a screen-based text editor that has been included with Unix and Unix-like operating systems for decades. It is available by default on nearly every Linux distribution, macOS, and BSD system, which makes it a reliable choice when you need to edit files on a remote server where graphical tools are not available.
Although vi is often associated with its improved successor, Vim (Vi Improved), the original vi remains a powerful utility in its own right. Its lightweight nature and universal availability mean that knowing how to edit in vi editor can save you during emergency system maintenance, SSH sessions, or when working inside minimal container environments Not complicated — just consistent..
Understanding the Two Primary Modes of Vi
The most important concept to grasp when learning how to edit in vi editor is that vi is a modal editor. This means the keyboard behaves differently depending on which mode you are in Surprisingly effective..
Command Mode
When you first open a file in vi, you are in command mode. You cannot simply start typing to add content. In this mode, every keypress is interpreted as a command rather than literal text. Instead, you use commands to deal with, delete, copy, and manipulate text Most people skip this — try not to..
Insert Mode
Insert mode is where you actually type and edit text, similar to how you would in Notepad or TextEdit. Even so, to enter insert mode from command mode, you press a key such as i, a, or o. To return to command mode, you press the Esc key That alone is useful..
Understanding when you are in command mode versus insert mode is the foundation of efficient vi editing. If you try to type text while in command mode, your keystrokes will trigger unexpected actions, which often confuses new users It's one of those things that adds up..
How to Launch and Exit the Vi Editor
To start editing a file, open your terminal and type:
vi filename
Replace filename with the name of the file you want to create or modify. If the file does not exist, vi will create it when you save Turns out it matters..
Exiting vi can be tricky for beginners because there is no visible “X” button or menu. You must use command-line commands entered from command mode. Here are the most common ways to exit:
:w— Save the file but keep vi open.:q— Quit vi if no changes have been made.:wq— Save changes and quit.:q!— Quit without saving changes.:x— Save and quit (similar to:wq).
Always press Esc first to ensure you are in command mode before typing any of these commands.
Step-by-Step Guide to Editing in Vi
Entering Insert Mode to Add Text
Once you have opened a file, you need to enter insert mode before you can type. From command mode, use one of the following keys:
i— Insert text before the current cursor position.a— Append text after the current cursor position.o— Open a new line below the current line and enter insert mode.O— Open a new line above the current line and enter insert mode.
After pressing one of these keys, you will see -- INSERT -- appear at the bottom of the screen in many versions of vi. You can now type freely. When you are finished, press Esc to return to command mode.
Moving the Cursor Without a Mouse
Because vi is designed for keyboard-only operation, you do not use arrow keys in command mode (although they often work in modern versions). Instead, use these efficient navigation keys:
h— Move left one