vi

text editorsLinux/Unix
The vi command is one of the most frequently used commands in Linux/Unix-like operating systems. vi Screen-oriented (visual) text editor

Quick Reference

Command Name:

vi

Category:

text editors

Platform:

Linux/Unix

Basic Usage:

vi [options] [arguments]

Common Use Cases

    Syntax

    vi [options] [file...]

    Options

    Option Description
    -b Edit in binary mode
    -c cmd Execute command cmd after loading the first file
    -d Start in diff mode (vimdiff)
    -e Start in Ex mode (like "ex")
    -h Display help message and exit
    -i VIMINFO Use VIMINFO file instead of .viminfo
    -L Same as -r (recover mode)
    -n No swap file, changes only in memory
    -o[N] Open N windows horizontally (default: one for each file)
    -O[N] Open N windows vertically (default: one for each file)
    -p[N] Open N tab pages (default: one for each file)
    -r List swap files or recover a crashed session
    -R Read-only mode (like "view")
    -s SCRIPTFILE Source SCRIPTFILE after loading the first file
    -t TAG Edit the file containing TAG
    -v Start in Vi mode (default)
    -x Edit encrypted files (will prompt for key)
    +[num] Start at line number [num]
    +/pattern Start at the first occurrence of pattern
    +cmd Execute command cmd after loading the first file
    -- End of options, only file names follow

    Common vi Commands

    Vi operates in multiple modes. The main ones are:

    • Normal mode: For navigation and simple editing (default)
    • Insert mode: For inserting text
    • Command-line mode: For executing longer commands
    • Visual mode: For selecting text

    Basic Navigation (Normal Mode)

    Command Description
    hMove cursor left
    jMove cursor down
    kMove cursor up
    lMove cursor right
    wMove to next word
    bMove to previous word
    0Move to beginning of line
    $Move to end of line
    ggMove to first line of file
    GMove to last line of file
    {number}GMove to line {number}
    Ctrl+fPage down
    Ctrl+bPage up

    Editing (Insert Mode)

    Command Description
    iInsert before cursor
    IInsert at beginning of line
    aAppend after cursor
    AAppend at end of line
    oOpen new line below
    OOpen new line above
    EscExit insert mode

    Deleting and Changing Text

    Command Description
    xDelete character under cursor
    XDelete character before cursor
    ddDelete current line
    {number}ddDelete {number} lines
    dwDelete word
    DDelete from cursor to end of line
    cwChange word (delete word and enter insert mode)
    ccChange line (delete line and enter insert mode)
    CChange from cursor to end of line
    rReplace single character
    REnter replace mode

    Copy and Paste

    Command Description
    yyYank (copy) current line
    {number}yyYank {number} lines
    ywYank word
    pPaste after cursor
    PPaste before cursor

    Search and Replace

    Command Description
    /patternSearch forward for pattern
    ?patternSearch backward for pattern
    nRepeat search in same direction
    NRepeat search in opposite direction
    :%s/old/new/gReplace all occurrences of "old" with "new" in file
    :s/old/new/gReplace all occurrences of "old" with "new" in current line
    :%s/old/new/gcReplace all occurrences with confirmation

    File Operations

    Command Description
    :wWrite (save) file
    :w filenameSave as filename
    :qQuit
    :q!Quit without saving
    :wqWrite and quit
    ZZWrite and quit (shortcut)
    :e filenameEdit another file
    :e!Reload current file (discard changes)

    Visual Mode

    Command Description
    vEnter visual mode (character-wise)
    VEnter visual mode (line-wise)
    Ctrl+vEnter visual block mode

    Miscellaneous

    Command Description
    uUndo
    Ctrl+rRedo
    .Repeat last command
    :set numberShow line numbers
    :syntax onEnable syntax highlighting
    Ctrl+gShow file status

    Examples

    How to Use These Examples

    The examples below show common ways to use the vi command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Open a file for editing vi file.txt
    # Open multiple files vi file1.txt file2.txt file3.txt
    # Open a file at a specific line number vi +10 file.txt
    # Open a file and position cursor at the first occurrence of a pattern vi +/pattern file.txt
    # Start vi in read-only mode vi -R file.txt
    # Open a new file and immediately start in insert mode vi +i newfile.txt # Advanced Examples Advanced # Open a file and execute commands on startup vi -c "set number" -c "syntax on" file.txt # Recover a file after a crash vi -r file.txt # Edit a file via remote connection vi scp://user@remote.host:/path/to/file # Split window horizontally and edit two files vi -o file1.txt file2.txt # Split window vertically and edit two files vi -O file1.txt file2.txt # Diff mode to compare two files vi -d file1.txt file2.txt # Start vi with a specific window size vi -geometry 80x40 file.txt # Use a specific swap file vi -S swapfile file.txt # Start vi with a specific color scheme vi -c "colorscheme desert" file.txt # Use vi to encrypt a file (will prompt for key) vi -x secret.txt

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `vi` (Visual Editor) is one of the most classic and ubiquitous text editors in Unix and Linux systems. Developed by Bill Joy in 1976, it was originally part of the Berkeley Software Distribution (BSD) of Unix. Today, what most people refer to as `vi` is actually often `vim` (Vi IMproved), a vastly enhanced version of the original vi, though backward compatible with it. **Core Characteristics:** 1. **Modal Editing:** Vi's most distinctive feature is its modal nature. Unlike most modern text editors where typing immediately inserts text, vi operates in different modes: - **Normal mode** (default): For navigation and commands - **Insert mode**: For adding and editing text - **Command-line mode**: For executing editor commands - **Visual mode** (in vim): For selecting blocks of text 2. **Keyboard-Centric:** Vi was designed in an era before mice were common, so it's entirely keyboard-driven. This makes it extremely efficient for touch typists who don't need to move their hands from the keyboard. 3. **Minimal Screen Usage:** Vi was designed for low-bandwidth terminal connections, so it minimizes screen updates and uses the available display space efficiently. 4. **Ubiquity:** Vi or a vi-compatible editor is available on virtually every Unix-like system, making it a crucial tool for system administrators who need to edit files on various systems. **Historical Significance:** The original vi was closely tied to the ex line editor, which itself evolved from the even older ed editor. Vi added visual capabilities to what was originally a line-oriented editing paradigm. This heritage explains some of vi's seemingly cryptic commands and its modal nature. **Modern Implementations:** 1. **Vim (Vi Improved):** The most popular enhanced implementation of vi, adding features like syntax highlighting, split windows, visual mode, and extensive customization options. When most people use "vi" today, they're actually using vim. 2. **Neovim:** A modern refactoring of Vim focused on extensibility and usability, with better plugin support and modern features. 3. **nvi:** A more traditional implementation aimed at closely following the original vi specification. **Strengths and Use Cases:** 1. **Remote System Administration:** Vi is ideal for editing files on remote servers or systems with minimal resources, as it works well over SSH connections and uses minimal system resources. 2. **Speed and Efficiency:** For experienced users, vi's modal editing and keyboard shortcuts allow for extremely fast editing once the learning curve is overcome. 3. **Customizability:** Modern vi implementations like vim offer extensive customization options through configuration files (.vimrc) and plugins. 4. **Availability:** Since some form of vi is available on virtually all Unix-like systems, learning vi ensures you can edit files on any system you encounter. **Learning Curve Considerations:** Vi is notorious for its steep learning curve, primarily due to its modal nature and command syntax that can seem arcane to beginners. However, many users find that the initial investment in learning vi pays off in long-term productivity gains. A helpful approach for beginners is to: 1. Start with basic operations (navigating, inserting text, saving, quitting) 2. Learn a few new commands each day rather than trying to memorize everything at once 3. Use a graphical vim implementation or a "vim tutor" program to ease the learning process **Practical Tips for Modern Users:** 1. **Configuration:** Modern vi implementations can be configured to be more user-friendly with syntax highlighting, line numbers, and other visual aids. 2. **Vim as an IDE:** With the right plugins, vim can function as a powerful integrated development environment for coding. 3. **Hybrid Approach:** Many modern text editors and IDEs offer "vi mode" or plugins that allow you to use vi keybindings within a more modern interface. Despite being over 45 years old, vi remains relevant and widely used because of its efficiency, ubiquity, and the speed it offers to experienced users. It represents a different philosophy of text editing focused on keyboard efficiency and minimal resource usage, contrasting with but complementing more graphical, mouse-driven editors.

    Related Commands

    These commands are frequently used alongside vi or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the vi command works in different scenarios.

    $ vi
    View All Commands