Home > Text Processing & Search > vi

vi: A Powerful Text Editor

vi is a powerful, mode-based text editor widely used on Unix and Linux systems. It allows users to create, edit, and save files by switching between command mode and insert mode, making it an essential tool for system administration and programming tasks.

Overview

vi is a tool for efficiently editing text files, particularly useful in server environments where GUI is not available. While it has a learning curve, mastering it allows for very fast editing.

Key Features

  • Mode-based editing (Command mode, Insert mode, Line mode, etc.)
  • Powerful search and replace functionality
  • Macro and script support
  • Efficient use of system resources

Key Options

These are the main options you can use when executing the vi command.

Opening Files and Modes

Generated command:

Try combining the commands.

Description:

`vi` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Here are basic examples of using the vi editor. Since vi is mode-based, you switch from command mode to insert mode (using i, a, o, etc.), and after editing, return to command mode to save and exit (:).

Create a New File or Open an Existing One

vi new_file.txt

Opens the file with the specified name, or creates a new one if it doesn't exist.

Open a File in Read-Only Mode

vi -R important_config.conf

Opens the file in read-only mode, where you cannot modify its content.

Open a File and Go to a Specific Line

vi +50 script.sh

Opens the file and moves the cursor to the 50th line.

Save and Exit

vi my_document.txt
# (After editing content)
# Press ESC key, then
:wq

After editing a file, type `:wq` in command mode to save and exit.

Exit Without Saving

vi temp_file.log
# (After editing content)
# Press ESC key, then
:q!

Forcefully exits without saving any changes made to the file.

Tips & Precautions

vi is powerful, but its mode-based nature can be challenging for beginners. Use the following tips to become familiar with using vi.

The Core of vi: Understanding Modes

vi operates primarily in two modes:

  • Command Mode: This is the default mode when you launch vi. You perform editing commands like file navigation, text deletion/copying/pasting, and command execution. Keystrokes are interpreted directly as commands.
  • Insert Mode: This is the mode for typing text directly. You enter Insert Mode from Command Mode by pressing keys like 'i' (insert), 'a' (append), or 'o' (open line). Press the 'ESC' key to return to Command Mode.

Frequently Used Basic Commands (in Command Mode)

  • i: Enter Insert Mode at the current cursor position
  • a: Enter Insert Mode after the current cursor position
  • o: Open a new line below the current line and enter Insert Mode
  • dd: Delete the current line
  • yy: Copy the current line
  • p: Paste the copied/deleted content
  • /: Search for text (e.g., `/search_term`)
  • :w: Save the file
  • :q: Quit (will warn if there are unsaved changes)
  • :wq: Save and quit
  • :q!: Force quit without saving

Tips for Learning vi

Most Linux systems include a vi/vim tutorial called `vimtutor`. Running `vimtutor` in your terminal provides an interactive way to learn the basics of vi. This is one of the best ways to get accustomed to vi.


Same category commands