Skip to content

✏️ Vim

"Vim is hard at first, but once you learn the basics, you'll love it."


Cheat Sheet

📑 Table of Contents


🎯 Introduction

Vim is a powerful, lightweight terminal text editor available on virtually every Linux system. It's very worth learning for DevOps work.

bash
# Install vim
sudo apt install vim

# Open or create a file
vim filename.txt

# Learn vim interactively
vimtutor

💡 Tip: Run vimtutor for an interactive tutorial built into vim.


🔄 Vim Modes

Vim operates in three main modes:

ModePurposeHow to Enter
CommandNavigate, delete, copy, pasteEsc (default mode)
InsertType and edit text freelyi, a, o, etc.
Last-LineSave, quit, search, settings: (colon)

When you open a file, you start in Command Mode.

┌─────────────────────────────────────────────┐
│                                             │
│              COMMAND MODE                   │
│           (default on open)                 │
│                                             │
│      i, a, o, etc.          :               │
│           ↓                  ↓              │
│    ┌──────────────┐   ┌──────────────┐      │
│    │ INSERT MODE  │   │ LAST-LINE    │      │
│    │  (editing)   │   │   MODE       │      │
│    └──────────────┘   └──────────────┘      │
│           ↑                  ↑              │
│          Esc               Esc              │
│                                             │
└─────────────────────────────────────────────┘

✍️ Entering Insert Mode

From Command Mode, use these keys to enter Insert Mode:

KeyAction
iInsert before cursor
IInsert at beginning of line
aAppend after cursor
AAppend at end of line
oOpen new line below
OOpen new line above

Press Esc to return to Command Mode.


⌨️ Command Mode Basics

These commands work in Command Mode (press Esc first):

Character Operations

KeyAction
xDelete character under cursor
rReplace character under cursor

Movement

KeyAction
hMove left
jMove down
kMove up
lMove right
wJump to next word
bJump to previous word
0Jump to beginning of line
$Jump to end of line

💾 Last-Line Mode (Saving & Quitting)

Press : to enter Last-Line Mode, then type a command:

CommandAction
:wSave (write) file
:qQuit (fails if unsaved changes)
:q!Quit without saving (force)
:wqSave and quit
ZZSave and quit (from Command Mode)
:xSave and quit (only writes if changed)

🔍 Searching & Replacing

Searching

CommandAction
/patternSearch forward
?patternSearch backward
nNext match
NPrevious match

Search & Replace

vim
:%s/old/new/g
PartMeaning
%Apply to all lines
sSubstitute command
oldPattern to find
newReplacement text
gGlobal (all occurrences per line)
vim
:%s/foo/bar/g      " Replace all 'foo' with 'bar'
:%s/foo/bar/gc     " Replace with confirmation
:s/foo/bar/g       " Replace only on current line

Run External Commands

vim
:!ls               " Run ls command
:!pwd              " Show current directory

↩️ Undo & Redo

CommandAction
uUndo last change
Ctrl + rRedo last undone change
:e!Revert to last saved version

✂️ Cut, Copy & Paste

Quick Cut & Paste

CommandAction
ddCut (delete) current line
5ddCut 5 lines
10ddCut 10 lines
yyCopy (yank) current line
5yyCopy 5 lines
pPaste below cursor
PPaste above cursor

Visual Selection

CommandAction
vStart character selection
VStart line selection
Ctrl + vStart block (column) selection

After selecting:

  • y — Copy (yank) selection
  • d — Cut (delete) selection
  • p — Paste

🧭 Navigation

Moving Within File

CommandAction
ggGo to beginning of file
GGo to end of file
:100Go to line 100
Ctrl + fPage down
Ctrl + bPage up

📂 Working with Multiple Files

Opening Multiple Files

bash
vim file_a file_b           # Open files in sequence
vim -o file_a file_b        # Open in horizontal split
vim -O file_a file_b        # Open in vertical split
vim -d file_a file_b        # Open in diff mode (vimdiff)
CommandAction
:n or :nextGo to next file
:N or :prevGo to previous file
:lsList open buffers

Comparing Files (Diff Mode)

bash
vim -d file_a file_b
# or
vimdiff file_a file_b

Highlights differences between files — useful for comparing configs or code.

⚠️ Warning: If vim closes unexpectedly (crash, terminal closed), a swap file (.swp) is created. You'll see a recovery prompt next time you open the file. Always exit vim properly with :wq or :q!.


⚙️ Settings & Configuration

Temporary Settings (Current Session)

CommandAction
:set nuShow line numbers
:set nonuHide line numbers
:syntax onEnable syntax highlighting
:syntax offDisable syntax highlighting

Permanent Settings

Edit ~/.vimrc in your home directory to set global vim settings:

vim
" ~/.vimrc example
set number          " Show line numbers
syntax on           " Enable syntax highlighting
set tabstop=4       " Tab width
set expandtab       " Use spaces instead of tabs
set autoindent      " Auto-indent new lines

📚 Quick Reference Card

Mode Switching

Esc             → Command Mode
i, a, o         → Insert Mode
:               → Last-Line Mode

Essential Commands

:w              → Save
:q              → Quit
:wq or ZZ       → Save and quit
:q!             → Quit without saving
h j k l         → Left, down, up, right
gg              → Beginning of file
G               → End of file
:100            → Go to line 100

Editing

i               → Insert before cursor
a               → Append after cursor
o               → New line below
x               → Delete character
dd              → Delete line
yy              → Copy line
p               → Paste
u               → Undo
Ctrl + r        → Redo

Search & Replace

/pattern        → Search forward
?pattern        → Search backward
n / N           → Next / previous match
:%s/old/new/g   → Replace all

Multiple Files

:n              → Next file
:N              → Previous file
vim -d a b      → Diff mode

📝 Note: Vim has a steep learning curve, but mastering even the basics will make you significantly more productive. Run vimtutor for interactive practice!

Released under the MIT License.