CyberCheatsheets

Vim Cheat Sheet

Vim editor commands — modes, motions, editing, search/replace, and the survival basics for editing files on remote shells.

Utilities & ShellseditorproductivityterminalvimUpdated 2026-06-17

Overview

Vim is the editor you'll hit on almost every Linux box, often the only one available on a stripped-down shell. Knowing the basics—how to move, edit, save, and quit—is essential. Vim is modal: Normal mode for navigation/commands, Insert mode for typing, Visual mode for selection.

Authorized use only. Edit files only on systems you own or have permission to access.

Survival (quit/save)

Enter Insert mode (start typing)

i

Back to Normal mode

Esc

Save (write)

:w

Quit

:q

Save and quit

:wq  or  ZZ

Quit without saving (discard changes)

:q!

Movement (Normal mode)

Left, down, up, right

h j k l

Forward / back one word

w / b

Start / end of line

0 / $

Top / bottom of file

gg / G

Jump to line 42

:42

Half page up / down

Ctrl-u / Ctrl-d

Editing

Delete character under cursor

x

Delete (cut) current line

dd

Yank (copy) current line

yy

Paste after / before cursor

p / P

Undo / redo

u / Ctrl-r

Change word (delete + insert)

cw

Append at line end / open new line below

A / o

Search & replace

Search forward (n = next, N = previous)

/pattern

Search backward

?pattern

Replace all occurrences in the file

:%s/old/new/g

Replace all with confirm prompt

:%s/old/new/gc

Clear search highlight

:noh

Visual mode & blocks

Character-wise selection

v

Line-wise selection

V

Block (column) selection

Ctrl-v

Indent / unindent line

>> / <<

Useful one-liners

Open a file at a specific line

vim +42 file.txt

Run a shell command from inside vim

:!id

Read command output into the buffer

:r !ls -la

Save a file you opened without write perms (sudo trick)

:w !sudo tee %

Spawn a shell (handy if vim is a sudo GTFOBin)

:!/bin/sh

Tips

  • If you're stuck, press Esc then type :q! to bail out without saving.
  • :w !sudo tee % saves a file you forgot to open with sudo.
  • Combine counts with motions: 3dd deletes 3 lines, 5j moves down 5.
  • vim appears on GTFOBins — sudo vim -c ':!/bin/sh' is a classic privesc.

References

Aide-mémoires similaires