Skip to main content

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

  1. Open terminal and type vim
  2. You'll see the Vim welcome screen
  3. Type :help and press Enter to see help
  4. Type :q and press Enter to quit help
  5. Type :q again 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 Esc to 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, or Ctrl+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

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%/_vimrc or %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

  1. Create a text file with several paragraphs
  2. Practice moving with hjkl
  3. Use w and b to move by words
  4. Use 0 and $ to move to line boundaries
  5. Use gg and G to move to file boundaries

Exercise 2: Basic Editing

  1. Open a file with some text
  2. Practice entering insert mode with i, a, o
  3. Make some changes
  4. Practice saving with :w
  5. Practice undo with u

Exercise 3: Text Objects

  1. Create a file with words in quotes: "hello world"
  2. Practice ci" to change inside quotes
  3. Practice di" to delete inside quotes
  4. Try with other text objects like i(, i{, iw

Exercise 4: Visual Mode

  1. Open a file with multiple lines
  2. Practice selecting text with v
  3. Practice selecting lines with V
  4. Practice block selection with Ctrl+v
  5. 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 (type vimtutor in 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!