Skip to content

andrewbasterfield/vim-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 

Repository files navigation

VIM Cheat sheet

This document is structured to teach Vim's concepts, from basic survival to its powerful command grammar.


Part 1: The Absolute Basics

Everything you need to survive your first 5 minutes in Vim.

Insert Mode (How to type)

  • i: insert text before the cursor
  • a: append text after the cursor
  • I: Insert at the beginning of the line
  • A: Append at the end of the line
  • o: open a new line below and enter insert mode
  • O: Open a new line above and enter insert mode
  • Esc: Exit insert mode and return to Command Mode

Saving & Quitting (How to get out)

  • :w: write (save) the file
  • :q: quit the current window/buffer
  • :wq: write (save) and quit
  • :q!: quit without saving (discard changes)

Undo/Redo (How to fix mistakes)

  • u: undo the last change
  • Ctrl-r: redo the last undone change

Part 2: The Core of Vim - Command Mode Editing

This is where Vim's power lies. Commands are like sentences.

Basic Navigation (The "Nouns" of Vim)

These are the motions you can use alone, or combine with verbs.

  • h, j, k, l: left, down, up, right
  • w/b: move to start of next/previous word (by alphanumeric chars)
  • W/B: move by WORD (by whitespace)
  • e: move to the end of the current word
  • ^/$: move to the first non-whitespace character / end of the line
  • f{char}/F{char}: find character on the current line (forward/backward)
  • t{char}/T{char}: move till character on the current line (forward/backward)
  • ;/,: repeat the last f or t command (forward/backward)
  • gg/G: go to the first/last line of the file
  • H/M/L: go to High/ Middle/ Low part of the screen
  • ctl-u/ctl-d: move half a screen up/ down
  • %: jump to matching bracket (), {}, []

The Vim Grammar: verb + noun

Combine a verb (action) with a noun (motion or text object). Example: d (verb) + w (noun) = dw (delete word). You can add a number to repeat: d2w (delete 2 words).

Verbs (The Action)

Verb Action
d delete (acts like a "cut")
c change (deletes and enters Insert Mode)
y yank (copies)
>/< indent / dedent
gu/gU change to lowercase / uppercase

Nouns (The "Thing" to Act On)

You can combine any verb above with any noun below.

1. Nouns as Motions (from the navigation section above)

Motion Meaning Example: d + Motion Example: c + Motion
w to the next word dw (delete word) cw (change word)
$ to the end of the line d$ (delete to EOL) c$ (change to EOL)
} to the next paragraph d} (delete to para) c} (change to para)

2. Nouns as Text Objects Act on the "object" your cursor is inside. Use i for "inner" and a for "around".

Text Object Meaning Example: d + Object Example: c + Object
iw/aw inner/ around word diw / daw ciw / caw
i"/a" inner/ around " quotes di" / da" ci" / ca"
i(/a( inner/ around () parens di( / da( ci( / ca(
i{/a{ inner/ around {} braces di{ / da{ ci{ / ca{
it/at inner/ around tag dit / dat cit / cat

Visual Mode: Making Selections

Visual Mode is used to make selections of text. After selecting, you can use a verb on the selection, like d (delete) or y (yank).

  • v: enter visual mode (character-wise).
  • V: enter Visual mode (line-wise).
  • Ctrl-v: enter Visual block mode (for column-wise selection).
  • Esc: exit Visual mode.

The Dot (.) Command: Repeat Your Last Change

One of Vim's most powerful commands. It repeats the last single action that changed the text.

  • .: Repeat the last change (e.g., a dw command, a ciw change, or an x command).

Pasting & Simple Edits

Commands that don't fit the main grammar.

  • p/P: paste after/before the cursor
  • x: delete character under the cursor
  • r{char}: replace a single character
  • dd/yy: delete/yank the current line (a common shortcut)

Part 3: Advanced Tools

Search & Replace

  • /foo: search forwards for foo
  • ?bar: search backwards for bar
  • n/N: repeat search in the same/opposite direction
  • */#: search for the word currently under the cursor (forward/backward)
  • :s/foo/bar/g: substitute foo with bar globally on the current line

Macros: Recording and Replaying Actions

Record any sequence of commands and replay it to automate repetitive tasks.

  • q{a-z}: Start recording a macro into a register (e.g., qa). Press q again while recording to stop.
  • @{a-z}: Execute the macro from the specified register (e.g., @a).
  • @@: Repeat the last executed macro.

Workspace Management

Managing files, tabs, and split windows. The key relationship is: Tabs contain Windows, and Windows display Buffers.

Multi-buffers (files)

Think of a buffer as a file loaded into Vim's memory. It's the text itself. You can have many files open in the background as buffers, even if you can't see them. Note: For practical purposes, this is a 1-to-1 mapping. A file on disk corresponds to exactly one buffer. The only exception is a "scratch" buffer that you can create in memory which doesn't have a file yet.

  • :e {file}: edit a file in a new buffer
  • :bn: go to next buffer
  • :bp: go to previous buffer
  • :ls: list open buffers
  • :bd: buffer delete (close buffer)

Windows (splits)

A window is a viewport on your screen that displays a buffer. You can split your screen to create multiple windows, allowing you to view several different files—or even different parts of the same file—at once.

  • :sp {file}: split window horizontally
  • :vsp {file}: vertical split window
  • ctl-w + h,j,k,l: move to window left, down, up, right
  • ctl-w + w: cycle through windows

Tabs (workspaces)

A tab is a collection of one or more windows. Use tabs to organize your screen into different workspaces (e.g., a "frontend" tab with HTML/CSS windows, and a "backend" tab with Python windows).

  • :tabnew: open a new tab
  • gt or :tabn: go to tab next
  • gT or :tabp: go to tab previous
  • :tabclose: close current tab

Part 4: Power User Essentials

Registers (Vim's Clipboards)

Every delete, yank, and paste goes through a "register". Think of them as named clipboards.

  • ": prefix to specify a register
  • " + a + y → yank into register a
  • " + a + p → paste from register a
  • "+y: yank to system clipboard
  • "+p: paste from system clipboard

Marks & Jumps

Marks let you set "bookmarks" in text to quickly return.

  • ma: set mark a at cursor
  • 'a: jump to beginning of line with mark a
  • `a: jump to exact cursor position at mark a
  • '': jump back to previous position

Advanced Search & Replace

  • :%s/foo/bar/g: replace all foo with bar in the whole file
  • :%s/foo/bar/gc: replace with confirmation
  • :10,20s/foo/bar/g: replace in lines 10–20 only

Part 5: Beyond the Basics

Folding (Hiding/Showing Sections)

Use folds to collapse large sections of code or text.

  • zf{motion}: fold a region (e.g., zf} folds a paragraph)
  • zo: open (unfold)
  • zc: close (fold)
  • za: toggle fold

The .vimrc (Customizing Vim)

Your .vimrc file makes Vim "yours". A few useful options:

set number        " show line numbers
syntax on         " enable syntax highlighting
set expandtab     " use spaces instead of tabs
set shiftwidth=4  " indent by 4 spaces
set ignorecase    " case-insensitive search
set smartcase     " ...unless uppercase is used

Plugins (Extending Vim)

Vim has a huge plugin ecosystem. Use a plugin manager (e.g., vim-plug):

call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-surround'
Plug 'preservim/nerdtree'
call plug#end()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published