vim

text editorsLinux/Unix
The vim command is one of the most frequently used commands in Linux/Unix-like operating systems. vim Vi IMproved, a programmers text editor

Quick Reference

Command Name:

vim

Category:

text editors

Platform:

Linux/Unix

Basic Usage:

vim [options] [arguments]

Common Use Cases

    Syntax

    vim [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")
    -f Run in foreground (for gvim)
    -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
    -N Not compatible with vi (use Vim features)
    -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
    -u VIMRC Use VIMRC file instead of any .vimrc
    -v Start in Vi mode (default)
    -x Edit encrypted files (will prompt for key)
    --noplugin Don't load plugin scripts
    --servername NAME Become server NAME
    --remote FILE Edit FILE in a Vim server if possible
    +[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 vim Commands

    Vim 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)
    :saveas filenameSave as filename and edit that file

    Visual Mode

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

    Multiple Windows and Tabs

    Command Description
    :splitSplit window horizontally
    :vsplitSplit window vertically
    Ctrl+w hMove to the window on the left
    Ctrl+w jMove to the window below
    Ctrl+w kMove to the window above
    Ctrl+w lMove to the window on the right
    :tabnewCreate a new tab
    gtGo to next tab
    gTGo to previous tab
    {number}gtGo to tab {number}

    Miscellaneous

    Command Description
    uUndo
    Ctrl+rRedo
    .Repeat last command
    :set numberShow line numbers
    :syntax onEnable syntax highlighting
    :set autoindentEnable auto-indentation
    :set tabstop=4Set tab width to 4 spaces
    :helpOpen help
    :help keywordGet help on keyword

    Vim-Specific Features

    Feature Description
    :NERDTreeOpen file browser (if plugin installed)
    :terminalOpen terminal window (Vim 8+)
    :PluginInstallInstall plugins (if using Vundle)
    :AirlineTheme themeChange airline theme (if plugin installed)
    :fzfFuzzy file finder (if plugin installed)

    Examples

    How to Use These Examples

    The examples below show common ways to use the vim 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 vim file.txt
    # Open multiple files vim file1.txt file2.txt file3.txt
    # Open a file at a specific line number vim +10 file.txt
    # Open a file and position cursor at the first occurrence of a pattern vim +/pattern file.txt
    # Start vim in read-only mode vim -R file.txt
    # Open a new file and immediately start in insert mode vim +i newfile.txt # Advanced Examples Advanced # Open a file and execute commands on startup vim -c "set number" -c "syntax on" file.txt # Recover a file after a crash vim -r file.txt # Edit a file via remote connection vim scp://user@remote.host:/path/to/file # Split window horizontally and edit two files vim -o file1.txt file2.txt # Split window vertically and edit two files vim -O file1.txt file2.txt # Diff mode to compare two files vim -d file1.txt file2.txt # Start vim with a specific colorscheme vim -c "colorscheme desert" file.txt # Use vim to encrypt a file (will prompt for key) vim -x secret.txt # Start vim in server mode with name vim --servername MYSERVER file.txt # Open multiple files in tabs vim -p file1.txt file2.txt file3.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

    Vim (Vi IMproved) is an enhanced version of the classic Unix text editor vi, offering significant improvements while maintaining compatibility with vi's commands. Created by Bram Moolenaar in 1991, Vim has grown to become one of the most powerful and versatile text editors available today. **Key Improvements Over Vi:** 1. **Enhanced Features:** Vim adds a multitude of features not present in the original vi, including syntax highlighting, a comprehensive help system, native scripting language (Vimscript), integrated spell checking, code folding, and diff mode. 2. **Extensibility:** Through its plugin architecture, Vim can be customized with thousands of community-developed plugins that add functionality ranging from language-specific features to complex IDE-like capabilities. 3. **Cross-Platform:** Unlike the original vi which was primarily Unix-based, Vim is available on virtually all platforms including Windows, macOS, Linux, and many others. 4. **Multi-Level Undo:** Vim provides a much more sophisticated undo/redo system compared to vi, allowing for branching undo trees and persistent undo history. 5. **Visual Mode:** Vim introduced visual mode, which makes selecting and operating on blocks of text much more intuitive than in the original vi. **Core Characteristics:** 1. **Modal Editing:** Like vi, Vim is based on the concept of modes, primarily: - **Normal mode** - For navigation and commands - **Insert mode** - For adding and editing text - **Visual mode** - For selecting text - **Command-line mode** - For entering commands 2. **Keyboard Efficiency:** Vim is designed to maximize editing speed by minimizing hand movement, using the home row keys for common operations, and employing compound commands that can be combined in powerful ways. 3. **Customizability:** Nearly every aspect of Vim can be customized through the .vimrc configuration file and through plugins. 4. **Macros and Registers:** Vim allows recording and playback of complex operations through macros, and uses registers to store text and commands for later use. **Modern Features and Extensions:** 1. **Modern UI Options:** Vim has evolved to include options for modern UI elements like status lines, tabs, and even GUI versions (gVim, MacVim). 2. **Language Support:** Through community plugins, Vim offers excellent support for hundreds of programming languages, markup formats, and file types. 3. **IDE Integration:** Vim can be configured to provide IDE-like features such as code completion, linting, debugging, and version control integration. 4. **Neovim Fork:** The Neovim project has forked Vim to modernize its codebase and add features like asynchronous operations, embedded terminal, and better plugin API. **Strengths and Use Cases:** 1. **Programming and Software Development:** Vim excels as a code editor due to its precision editing capabilities, extensibility, and support for nearly all programming languages. 2. **System Administration:** Vim is invaluable for system administrators who need to edit configuration files on remote servers, often over SSH connections. 3. **Text Processing:** For tasks involving complex text manipulation, Vim's commands and regular expression support make it particularly powerful. 4. **Cross-Platform Editing:** Users who work across multiple operating systems appreciate Vim's consistency across platforms. **Learning Curve and Resources:** Vim is notorious for its steep learning curve, but this initial investment pays off in long-term productivity. Resources for learning Vim include: 1. The built-in vimtutor program (type `vimtutor` in your terminal) 2. Vim's extensive :help system 3. Interactive websites like vimadventures.com and vim-adventures.com 4. Numerous books, including "Practical Vim" by Drew Neil **The Vim Philosophy:** At its core, Vim embodies a philosophy of efficient text editing that focuses on: 1. **Precision:** Commands operate on well-defined text objects like words, sentences, paragraphs, and blocks. 2. **Composability:** Simple commands can be combined to perform complex operations. 3. **Minimalism:** The editor should do one thing—edit text—extremely well. 4. **Keyboard Focus:** Keeping hands on the keyboard at all times maximizes efficiency. These principles have influenced many other tools and editors, with "vim modes" now available in numerous other editors and IDEs, showing the enduring impact of Vim's design philosophy.

    Related Commands

    These commands are frequently used alongside vim 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 vim command works in different scenarios.

    $ vim
    View All Commands