Getting Started with Vim
Comprehensive guide to installing, configuring, and learning Vim from scratch.
What is Vim?
Vim (Vi Improved) is a highly configurable text editor built to make creating and changing any kind of text very efficient. It includes many features that make it popular among programmers and power users:
- Modal editing: Different modes for different tasks
- Keyboard-driven: Minimal mouse usage required
- Highly customizable: Extensible through configuration and plugins
- Efficient: Fast editing once you learn the commands
- Ubiquitous: Available on virtually every Unix system
Installation
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install vim
Linux (CentOS/RHEL/Fedora)
# CentOS/RHEL
sudo yum install vim
# Fedora
sudo dnf install vim
macOS
# Using Homebrew
brew install vim
# Using MacPorts
sudo port install vim
Windows
- Download from vim.org
- Install via Chocolatey:
choco install vim - Use Windows Subsystem for Linux (WSL)
Building from Source
git clone https://github.com/vim/vim.git
cd vim/src
make
sudo make install
First Launch
Starting Vim
vim # Start with empty buffer
vim filename # Open specific file
vim -R filename # Open in read-only mode
vim -c command # Execute command on startup
Your First Vim Session
- Open terminal and type
vim - You'll see the Vim welcome screen
- Type
:helpand press Enter to see help - Type
:qand press Enter to quit help - Type
:qagain to quit Vim
Understanding Vim Modes
Normal Mode (Command Mode)
- Default mode when you start Vim
- Used for navigation and text manipulation
- Commands don't appear on screen
- Press
Escto return to normal mode
Insert Mode
- Used for typing text
- Text appears as you type
- Enter with
i,a,o, etc. - Exit with
Esc
Visual Mode
- Used for selecting text
- Enter with
v,V, orCtrl+v - Selected text is highlighted
- Exit with
Esc
Command-line Mode
- Used for commands and searches
- Enter with
:,/, or? - Commands appear at bottom of screen
- Execute with
Enter
Basic Commands
Navigation
h " Move left
j " Move down
k " Move up
l " Move right
w " Move to next word
b " Move to previous word
0 " Move to beginning of line
$ " Move to end of line
gg " Go to first line
G " Go to last line
Editing
i " Insert before cursor
a " Insert after cursor
o " Open new line below
O " Open new line above
x " Delete character
dd " Delete line
yy " Copy line
p " Paste
u " Undo
File Operations
:w " Save file
:q " Quit
:wq " Save and quit
:q! " Quit without saving
:e file " Edit file
Creating Your First .vimrc
The .vimrc file is Vim's configuration file. Create it in your home directory:
Basic .vimrc
" Basic settings
set number " Show line numbers
set relativenumber " Show relative line numbers
set hlsearch " Highlight search results
set incsearch " Incremental search
set ignorecase " Case-insensitive search
set smartcase " Case-sensitive if uppercase used
set autoindent " Auto-indent new lines
set expandtab " Use spaces instead of tabs
set tabstop=4 " Tab width
set shiftwidth=4 " Indent width
set softtabstop=4 " Soft tab width
set showmatch " Show matching brackets
set ruler " Show cursor position
set wildmenu " Enhanced command completion
set laststatus=2 " Always show status line
set backspace=2 " Allow backspace in insert mode
set encoding=utf-8 " Set encoding
set fileencoding=utf-8 " Set file encoding
" Enable syntax highlighting
syntax on
" Enable filetype detection
filetype plugin indent on
" Color scheme
colorscheme desert
" Key mappings
let mapleader = "," " Set leader key
nnoremap <leader>w :w<CR> " Quick save
nnoremap <leader>q :q<CR> " Quick quit
nnoremap <leader>h :noh<CR> " Clear search highlight
" Make j and k work with wrapped lines
nnoremap j gj
nnoremap k gk
" Quick window navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Location of .vimrc
- Linux/macOS:
~/.vimrc - Windows:
%USERPROFILE%/_vimrcor%USERPROFILE%/.vimrc
Applying Changes
After editing .vimrc:
:source ~/.vimrc " Reload configuration
Essential Vim Concepts
The Vim Way
Vim follows the principle of composability:
- Operators: Actions like delete (
d), yank (y), change (c) - Motions: Movements like word (
w), line ($), paragraph (}) - Combine them:
dw(delete word),y$(yank to end of line)
Counting
Most commands accept a count:
3w " Move 3 words forward
5dd " Delete 5 lines
2yy " Copy 2 lines
Repetition
. " Repeat last command
@@ " Repeat last macro
Text Objects
Vim understands text structures:
iw " Inner word
aw " A word (including space)
i" " Inside quotes
a( " Around parentheses
ip " Inner paragraph
Examples:
diw " Delete inner word
ci" " Change inside quotes
ya( " Yank around parentheses
Learning Path
Week 1: Basics
- Practice moving with
hjkl - Learn to switch between modes
- Practice basic editing commands
- Create and edit your
.vimrc
Week 2: Navigation
- Learn word movement (
w,b,e) - Practice line movement (
0,$,^) - Use search for navigation (
/,?) - Learn to jump to lines (
:42,42G)
Week 3: Editing
- Master text objects (
iw,i",i() - Practice visual mode selections
- Learn copy/paste operations
- Understand change and delete commands
Week 4: Efficiency
- Learn to use counts with commands
- Practice the dot command (
.) - Start using macros
- Learn window management
Common Beginner Mistakes
1. Staying in Insert Mode
Problem: Treating Vim like a regular text editor Solution: Return to normal mode frequently, use normal mode for navigation
2. Using Arrow Keys
Problem: Reaching for arrow keys instead of hjkl
Solution: Disable arrow keys in .vimrc:
" Disable arrow keys
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
3. Not Using Text Objects
Problem: Selecting text character by character
Solution: Learn and use text objects like iw, i", i(
4. Ignoring the Dot Command
Problem: Manually repeating commands
Solution: Use . to repeat last command
5. Not Learning Motions
Problem: Moving cursor inefficiently Solution: Learn word movement, search, and jump commands
Getting Help
Built-in Help
:help " General help
:help command " Help for specific command
:help 'option' " Help for option (note quotes)
:help :command " Help for ex command
Help Navigation
Ctrl+] " Follow link
Ctrl+T " Go back
:q " Close help
Useful Help Topics
:help quickref " Quick reference
:help index " Command index
:help user-manual " User manual
:help tips " Tips and tricks
Practice Exercises
Exercise 1: Basic Navigation
- Create a text file with several paragraphs
- Practice moving with
hjkl - Use
wandbto move by words - Use
0and$to move to line boundaries - Use
ggandGto move to file boundaries
Exercise 2: Basic Editing
- Open a file with some text
- Practice entering insert mode with
i,a,o - Make some changes
- Practice saving with
:w - Practice undo with
u
Exercise 3: Text Objects
- Create a file with words in quotes:
"hello world" - Practice
ci"to change inside quotes - Practice
di"to delete inside quotes - Try with other text objects like
i(,i{,iw
Exercise 4: Visual Mode
- Open a file with multiple lines
- Practice selecting text with
v - Practice selecting lines with
V - Practice block selection with
Ctrl+v - Try operations on selections (delete, copy, change)
Customization Tips
Color Schemes
:colorscheme desert " Change color scheme
:colorscheme blue " Try different scheme
Status Line
set statusline=%f\ %m\ %r\ Line:\ %l/%L\ Column:\ %c\ %p%%
Key Mappings
" Map F2 to save
nnoremap <F2> :w<CR>
" Map F3 to toggle line numbers
nnoremap <F3> :set number!<CR>
" Map jj to escape
inoremap jj <Esc>
Performance Tips
Startup Performance
" Add to .vimrc for faster startup
set nocompatible
set ttyfast
set lazyredraw
Large File Handling
" For large files
set synmaxcol=200 " Limit syntax highlighting
set maxmempattern=1000 " Limit memory for pattern matching
Useful Resources
Online Tutorials
vimtutor- Built-in tutorial (typevimtutorin terminal)- Interactive Vim Tutorial online
- Vim Adventures (gamified learning)
Books
- "Learning the vi and Vim Editors" by Arnold Robbins
- "Practical Vim" by Drew Neil
- "Modern Vim" by Drew Neil
Practice Sites
- vim-adventures.com
- openvim.com
- vimgenius.com
Community
- r/vim on Reddit
- #vim on Freenode IRC
- Vi/Vim Stack Exchange
This guide provides a solid foundation for getting started with Vim. Take your time to practice each concept before moving on to the next. Vim has a steep learning curve, but the efficiency gains are worth the investment!