vim (Vi IMproved)

Advanced modal text editor with extensive features and customization

Syntax: vim [options] [file...]
Note: vim is an enhanced version of the vi editor with many additional features including syntax highlighting, multiple undo levels, split windows, plugins, and extensive customization options.

Description

vim (Vi IMproved) is a highly configurable text editor built to enable efficient text editing. It's an improved version of the vi editor distributed with most UNIX systems. vim includes many features that make it particularly useful for editing programs and configuration files.

Key vim Enhancements over vi

Feature Description
Syntax Highlighting Color-coded syntax for many programming languages
Multiple Undo/Redo Unlimited undo levels with undo tree
Visual Mode Select text visually before operating on it
Split Windows Edit multiple files simultaneously
Plugin System Extensive plugin ecosystem
Auto-completion Built-in completion for words, filenames, etc.
Folding Hide/show sections of code
Macros Record and replay command sequences

Visual Mode

Visual mode commands:
# Enter visual modes
v          # Character-wise visual mode
V          # Line-wise visual mode
Ctrl+v     # Block-wise visual mode

# Visual mode operations
d          # Delete selected text
y          # Copy (yank) selected text
c          # Change selected text
>          # Indent selected lines
<          # Unindent selected lines
:          # Execute command on selection

Window Management

Split windows:
# Horizontal split
:split filename
:sp filename

# Vertical split
:vsplit filename
:vsp filename

# Navigate between windows
Ctrl+w h   # Move to left window
Ctrl+w j   # Move to window below
Ctrl+w k   # Move to window above
Ctrl+w l   # Move to right window

# Resize windows
Ctrl+w +   # Increase height
Ctrl+w -   # Decrease height
Ctrl+w >   # Increase width
Ctrl+w <   # Decrease width

Advanced Editing Features

Multiple undo/redo:
# Undo/redo
u          # Undo last change
Ctrl+r     # Redo last undone change
:earlier 5m # Go back 5 minutes in time
:later 10s  # Go forward 10 seconds

# Undo tree navigation
:undolist   # Show undo tree
:undo 5     # Go to specific undo state
Macros:
# Record macro
qa         # Start recording macro in register 'a'
# ... perform actions ...
q          # Stop recording

# Play macro
@a         # Execute macro from register 'a'
@@         # Repeat last executed macro
5@a        # Execute macro 5 times

Search and Replace

Advanced search features:
# Search with regex
/\d\+      # Search for numbers
/\  # Search for whole word

# Case-sensitive search
/\Cpattern # Force case-sensitive
/\cpattern # Force case-insensitive

# Search and replace with confirmation
:%s/old/new/gc

# Search in visual selection
:'<,'>s/old/new/g

# Global commands
:g/pattern/d    # Delete all lines matching pattern
:v/pattern/d    # Delete all lines NOT matching pattern

File and Buffer Management

Buffer operations:
# Buffer commands
:ls        # List all buffers
:b 2       # Switch to buffer 2
:bn        # Next buffer
:bp        # Previous buffer
:bd        # Delete current buffer

# File operations
:e filename    # Edit file
:w filename    # Write to file
:sav filename  # Save as (and switch to new file)
:r filename    # Read file into current buffer

vim Configuration (.vimrc)

Basic .vimrc configuration:
" ~/.vimrc - vim configuration file

" Enable syntax highlighting
syntax on

" Show line numbers
set number

" Enable mouse support
set mouse=a

" Set tab width
set tabstop=4
set shiftwidth=4
set expandtab

" Enable auto-indent
set autoindent
set smartindent

" Show matching brackets
set showmatch

" Enable incremental search
set incsearch
set hlsearch

" Case-insensitive search
set ignorecase
set smartcase

" Enable file type detection
filetype plugin indent on

" Set color scheme
colorscheme desert

Plugins and Package Management

Popular vim plugins:
# Plugin managers
" Vim-Plug (modern plugin manager)
" Vundle (classic plugin manager)
" Pathogen (simple plugin manager)

# Popular plugins
" NERDTree - File explorer
" fzf.vim - Fuzzy finder
" vim-airline - Status line
" vim-fugitive - Git integration
" YouCompleteMe - Auto-completion
" vim-surround - Surround text objects
" vim-commentary - Comment/uncomment code

Code Folding

Folding commands:
# Manual folding
zf         # Create fold
zd         # Delete fold
zo         # Open fold
zc         # Close fold
za         # Toggle fold

# Fold navigation
zj         # Move to next fold
zk         # Move to previous fold
zR         # Open all folds
zM         # Close all folds

# Set fold method
:set foldmethod=indent
:set foldmethod=syntax
:set foldmethod=manual

Auto-completion

Built-in completion:
# In insert mode
Ctrl+n     # Next completion
Ctrl+p     # Previous completion
Ctrl+x Ctrl+f  # File name completion
Ctrl+x Ctrl+l  # Line completion
Ctrl+x Ctrl+o  # Omni completion (language-specific)

Command-line Options

Option Description
-n No swap file
-r Recovery mode
-R Read-only mode
-d Diff mode
+n Start at line n
-c command Execute command after loading

Practical Examples

Programming workflow:
# Open project files
vim -p *.py        # Open all Python files in tabs

# Navigate between tabs
gt                 # Next tab
gT                 # Previous tab
:tabnew filename   # Open new tab

# Code navigation
gd                 # Go to definition
Ctrl+o             # Go back
Ctrl+i             # Go forward
*                  # Search for word under cursor
Text manipulation:
# Sort lines
:sort              # Sort all lines
:'<,'>sort         # Sort selected lines

# Remove duplicates
:sort u            # Sort and remove duplicates

# Execute shell commands
:!ls               # Run ls command
:r !date           # Insert date output

# Filter text through external command
:%!sort            # Sort entire file using external sort

Learning Resources

Built-in help and tutorials:
# vim built-in help
:help              # General help
:help :command     # Help for specific command
:help i_CTRL-N     # Help for insert mode command

# Interactive tutorial
vimtutor           # Run from command line

# Practice commands
:help user-manual  # User manual
:help tips         # Tips and tricks

Common Use Cases

  • Software development: Writing and editing code with syntax highlighting
  • System administration: Editing configuration files
  • Text processing: Advanced text manipulation and formatting
  • Remote editing: Editing files over SSH connections
  • Large file editing: Efficiently handling large text files
  • Batch editing: Making similar changes across multiple files
  • Documentation: Writing and editing documentation
  • Data analysis: Processing and analyzing text-based data

Best Practices

  • Customize your .vimrc file to match your workflow
  • Learn vim gradually - master basics before advanced features
  • Use plugins judiciously - start with essential ones
  • Practice vim motions and commands regularly
  • Use visual mode for complex text selections
  • Learn to use vim's built-in help system
  • Use version control for your vim configuration
  • Explore vim's extensive customization options
Related Commands: vi, nano, emacs, less, more