NeoVIM


Getting Help

  • :h <search> - Get help on any topic
  • :h ^n - Search CTRL-N purpose in normal mode
  • :h i_^n - Search CTRL-N purpose in insert mode
  • :helpgrep <search> - Search through everything in the vim manual
  • :help tutor - Launch vim’s built-in tutorial

Essential Modes

Vim has 4 main modes you need to master:

Normal Mode (Default)

  • Purpose: Navigate and manipulate text efficiently
  • Enter: <Esc> from any mode
  • Key concept: Every keystroke is a command

Insert Mode

  • Purpose: Write text like a traditional editor
  • Enter: i (insert), a (append), o (new line below), O (new line above)
  • Exit: <Esc> to return to normal mode

Visual Mode

  • Purpose: Select text before operating on it
  • Enter: v (character), V (line), <C-v> (block)
  • Use: Select text, then perform operations (delete, copy, etc.)

Command Mode

  • Purpose: Execute complex commands and searches
  • Enter: : for commands, / for search, ? for reverse search
  • Use: File operations, search/replace, advanced text manipulation

Basic Navigation

Character Movement

  • h - Move left
  • j - Move down
  • k - Move up
  • l - Move right

Word Movement

  • w - Start of next word
  • e - End of current word
  • b - Beginning of current/previous word
  • W, E, B - Same as above but skip punctuation

Line Movement

  • 0 - Beginning of line
  • ^ - First non-whitespace character
  • $ - End of line
  • g_ - Last non-whitespace character

Screen Movement

  • H - Top of screen
  • M - Middle of screen
  • L - Bottom of screen
  • <C-u> - Page up
  • <C-d> - Page down
  • <C-b> - Full page up
  • <C-f> - Full page down

File Movement

  • gg or go - First line of file
  • G - Last line of file
  • :<number> - Go to line number
  • % - Go to matching bracket/parenthesis

Advanced Movement

Search Navigation

  • * - Next occurrence of word under cursor
  • # - Previous occurrence of word under cursor
  • n - Next search result
  • N - Previous search result

Advanced Line Navigation

  • f<char> - Find character on current line
  • F<char> - Find character backwards on current line
  • t<char> - Till character (one before)
  • T<char> - Till character backwards
  • ; - Repeat last f/F/t/T
  • , - Repeat last f/F/t/T backwards

Text Object Navigation

  • ( / ) - Previous/next sentence
  • { / } - Previous/next paragraph
  • [[ / ]] - Previous/next section

Special g Commands in Normal mode

  • gj / gk - Move by display lines (wrapped lines)
  • g0 / g$ - First/last character of display line
  • gf - Open file under cursor
  • gv - Re-select previous visual selection
  • go - Go to byte offset (neovim only)
  • gi - Insert text in the same position as where Insert mode was stopped last time in the current buffer
  • g; - Go to older position in change list
  • g, - Go to newer position in change list
  • g& - Synonym for :%s//~/& (repeat last substitute with last search pattern on all lines)
  • g* - Like ”*”, but don’t put ”<” and ”>” around the word.
  • g# - Like ”#”, but don’t put ”<” and ”>” around the word.
  • gI - Insert text in column 1 [count] times.

Global (:g) Commands in Ex command

The :g command executes an Ex command on all lines matching a pattern. It’s extremely powerful for batch editing:

  • :g/pattern/d — Delete all lines matching pattern
  • :g/pattern/p — Print all lines matching pattern
  • :g/pattern/normal A; — Append semicolon to matching lines
  • :g!/pattern/d — Delete all lines NOT matching pattern
  • :v/pattern/d — Same as above (inverse global)

Essential Editing

Basic Operations

  • i - Insert before cursor
  • a - Insert after cursor
  • I - Insert at beginning of line
  • A - Insert at end of line
  • o - Open new line below
  • O - Open new line above
  • s - Substitute character (delete and insert)
  • S - Substitute line (delete line and insert)

Deletion

  • x - Delete character under cursor
  • X - Delete character before cursor
  • dw - Delete word
  • dd - Delete line
  • D - Delete to end of line
  • d$ - Delete to end of line (same as D)
  • d0 - Delete to beginning of line
  • s/\s\+$// - delete all trailing spaces in empty lines

Change (Delete and Insert)

  • cw - Change word
  • cc - Change line
  • C - Change to end of line
  • c$ - Change to end of line (same as C)
  • r<char> - Replace single character
  • R - Replace mode (overwrite)

Copy (Yank) and Paste

  • yw - Yank word
  • yy - Yank line
  • Y - Yank to end of line
  • p - Paste after cursor/line
  • P - Paste before cursor/line

Undo and Redo

  • u - Undo last change
  • <C-r> - Redo
  • U - Undo all changes on current line

Repeat

  • . - Repeat last change
  • @@ - Repeat last macro

Search and Replace

  • /pattern - Search forward
  • ?pattern - Search backward
  • n - Next match
  • N - Previous match
  • * - Search word under cursor forward
  • # - Search word under cursor backward

Tips: In search mode, press <C-g> or <C-t> to select the next or previous search element without exiting the search mode, which can be handy if you do not want to fill up your jump list.

Search Options

  • /pattern\c - Case insensitive search
  • /pattern\C - Case sensitive search
  • /\<word\> - Search for whole word only

Basic Replace

  • :s/old/new/ - Replace first occurrence on current line
  • :s/old/new/g - Replace all occurrences on current line
  • :%s/old/new/g - Replace all occurrences in file
  • :%s/old/new/gc - Replace all with confirmation

Advanced Replace

  • :'<,'>s/old/new/g - Replace in visual selection
  • :5,10s/old/new/g - Replace in lines 5-10
  • :%s/old/new/gI - Case sensitive replace
  • :%s/\<old\>/new/g - Replace whole words only

Replace Shortcuts

  • & - Repeat last substitute command
  • g& - Repeat last substitute on all lines

Text Objects and Operators

Understanding Text Objects

Text objects define what text to operate on. Combine with operators for powerful editing.

Inner vs Around (i vs a)

  • iw - Inner word (excludes surrounding white space)
  • aw - Around word (includes surrounding white space)
  • i" - Inner quotes (text between quotes)
  • a" - Around quotes (includes the quotes)

Common Text Objects

  • w - Word
  • s - Sentence
  • p - Paragraph
  • t - Tag (HTML/XML)
  • " ' ` - Quoted strings
  • () [] {} <> - Bracketed text

Examples

  • diw - Delete inner word
  • cis - Change inner sentence
  • yap - Yank around paragraph
  • vi" - Select inner quoted text
  • da( - Delete around parentheses

Registers and Macros

Named Registers

  • "ayy - Yank line to register ‘a’
  • "Ayy - Append line to register ‘a’
  • "ap - Paste from register ‘a’
  • "_dd - Delete to black hole register (no save)
  • (Insert mode) <C-r>ap - Paste from register ‘a’

Special Registers

  • "+ - System clipboard
  • "* - Primary selection (Linux)
  • "0 - Last yank
  • "1-9 - Delete history
  • "% - Current filename
  • "/ - Last search pattern

Macros

  • qa - Start recording macro to register ‘a’
  • q - Stop recording
  • @a - Execute macro from register ‘a’
  • @@ - Repeat last macro
  • Q - Repeat last macro
  • 10@a - Execute macro 10 times
  • qA - Append actions to the macro ‘a’

Visual Mode Mastery

Visual Mode Types

  • v - Character-wise visual mode
  • V - Line-wise visual mode
  • <C-v> - Block-wise visual mode

Visual Mode Operations

  • o - Move to opposite end of selection
  • O - Move to opposite corner (block mode)
  • gv - Re-select last visual selection

Block Mode Tricks

  • <C-v> → select block → I → type → <Esc> - Insert at beginning of multiple lines
  • <C-v> → select block → $A → type → <Esc> - Append to end of multiple lines
  • <C-v> → select block → r<char> - Replace all selected characters

Insert Mode Enhancements

  • <C-h> - Delete character (like backspace)
  • <C-w> - Delete word
  • <C-u> - Delete to beginning of line
  • <C-o> - Execute one normal mode command
  • <C-r> - Insert from register
    • <C-r>+ - Paste clipboard
    • <C-r>0 - Paste last yank
    • <C-r>" - Paste default register
  • <C-n> / <C-p> - Autocomplete next/previous
  • <C-x><C-f> - File path completion
  • <C-x><C-l> - Line completion

Command Mode Power

Range Operations

  • :5,10d - Delete lines 5-10
  • :.,$s/old/new/g - Replace from current line to end
  • :%normal A; - Append semicolon to all lines
  • :'<,'>normal I// - Comment selected lines

Global Commands

  • :g/pattern/d - Delete all lines matching pattern
  • :g/pattern/p - Print all lines matching pattern
  • :g/pattern/normal A; - Append semicolon to matching lines
  • :g!/pattern/d - Delete all lines NOT matching pattern
  • :v/pattern/d - Same as above (inverse global)

Useful Commands

  • :sort - Sort selected lines
  • :sort! - Sort in reverse
  • :sort u - Sort and remove duplicates
  • :%!command - Filter entire file through external command
  • :'<,'>!sort - Sort selected lines using external sort

File Operations

Basic File Commands

  • :w - Save file
  • :w filename - Save as filename
  • :q - Quit
  • :q! - Force quit (discard changes)
  • :wq - Save and quit
  • :x - Save and quit (only if changes)
  • :wa - Save all open files

File Navigation

  • :e filename - Edit file
  • :e! - Reload current file
  • :bn / :bp - Next/previous buffer
  • :bd - Delete buffer
  • :ls - List buffers
  • <C-^> - Switch to alternate buffer

Window Management

Window Splits

  • :split / <C-w>s - Horizontal split
  • :vsplit / <C-w>v - Vertical split
  • <C-w>c - Close window
  • <C-w>o - Close all other windows

Window Navigation

  • <C-w>h/j/k/l - Move to left/down/up/right window
  • <C-w>w - Cycle through windows
  • <C-w>p - Go to previous window

Window Resizing

  • <C-w>= - Equalize window sizes
  • <C-w>+ / <C-w>- - Increase/decrease height
  • <C-w>> / <C-w>< - Increase/decrease width
  • <C-w>_ - Maximize height
  • <C-w>| - Maximize width

Spelling

  • :set spell — Enable spell checking
  • :set nospell — Disable spell checking
  • ]s — Next misspelled word
  • [s — Previous misspelled word
  • z= — Suggestions for correction
  • zg — Add word to dictionary
  • zw — Mark word as incorrect

Advanced Tips and Tricks

Efficiency Boosters

  • cgn - Change next search match (use with * then . to repeat)
  • gn - Select next search match (visual mode)
  • J - Join lines (remove line break)
  • gJ - Join lines without adding space
  • zz - Center current line on screen
  • zt - Move current line to top of screen
  • zb - Move current line to bottom of screen

Quick Edits

  • s - Substitute character (delete char and insert)
  • S - Substitute line (delete line and insert)
  • C - Change to end of line
  • D - Delete to end of line
  • cc - Change entire line
  • dd - Delete entire line

Text Transformation

  • gq - Format/wrap long lines
  • gu - Convert to lowercase
  • gU - Convert to uppercase
  • g~ - Toggle case
  • < / > - Indent/Un-indent (in visual mode)
  • == - Auto-indent current line
  • gg=G - Auto-indent entire file

Advanced Movement Patterns

  • f<char>; - Find character and repeat
  • t<char>, - Till character and reverse
  • ci" - Change inside quotes
  • da( - Delete around parentheses

Command Line Tricks

  • !!<command> - Replace current line with command output
  • :r !<command> - Insert command output below cursor
  • :.!<command> - Filter current line through command
  • :%!<command> - Filter entire file through command
  • :'<,'>!<command> - Filter selection through command

Quickfix List Power

  • :set makeprg=<command> - Set make command
  • :make - Run make command, populate quickfix
  • :cl - List all errors
  • :cc<number> - Jump to error number
  • :cn / :cp - Next/previous error
  • :cdo %s/old/new/g - Search and replace in quickfix results
  • :cfdo %s/old/new/g - Search and replace in quickfix files

Session Management

  • <C-z> - Suspend vim (return with fg)
  • :mksession session.vim - Save session
  • :source session.vim - Load session
  • vim -S session.vim - Start with session

Marks and Jumps

  • m<letter> - Set mark
  • '<letter> - Jump to mark line
  • `<letter> - Jump to exact mark position
  • '' - Jump to previous position
  • <C-o> - Jump to older position in jump list
  • <C-i> - Jump to newer position in jump list

Advanced Register Tricks

  • qaq - Clear register ‘a’
  • "_dd - Delete without saving to register
  • "+yy - Yank to system clipboard
  • "*yy - Yank to primary selection (Linux)
  • "%p - Paste current filename
  • "/p - Paste last search pattern

Folding

  • zf - Create fold (in visual mode)
  • za - Toggle fold
  • zo - Open fold
  • zc - Close fold
  • zR - Open all folds
  • zM - Close all folds

Diffing

  • :windo diffthis - Compare two windows
  • nvim -d file1 file2 - Start in diff mode
  • ]c / [c - Next/previous diff
  • do - Diff obtain (get change from other window)
    • :diffget <other_file> - take the content of other file
  • dp - Diff put (put change to other window)
    • :diffput <other_file> - put the content of to other file
  • :windo diffoff - close diff mode

Completion in Insert Mode

  • <C-n> - Next completion
  • <C-p> - Previous completion
  • <C-x><C-f> - File path completion
  • <C-x><C-l> - Whole line completion
  • <C-x><C-o> - Omni completion (language-specific)

Time-Saving Shortcuts

  • <C-a> - Increment number under cursor
  • <C-x> - Decrement number under cursor
  • ga - Show ASCII value of character
  • g8 - Show UTF-8 bytes of character
  • :retab - Convert tabs to spaces (or vice versa)
  • :set paste - Toggle paste mode for clean pasting

Alt Key Shortcuts (Insert Mode)

  • <M-o> - Insert newline below and stay in insert mode
  • <M-O> - Insert newline above and stay in insert mode
  • <M-S-a> - Go to end of line and stay in insert mode
  • <M-S-i> - Go to beginning of line and stay in insert mode

Quick Reference

Most Essential Commands

CommandActionCommandAction
iInsert mode<Esc>Normal mode
h j k lMove cursorw b eWord movement
xDelete charddDelete line
yyCopy linepPaste
uUndo<C-r>Redo
:wSave:qQuit
/textSearchnNext match
*Find wordcgnChange match
.Repeat%Match bracket

Text Objects (use with d, c, y, v)

ObjectDescriptionObjectDescription
iwInner wordawAround word
i"Inside quotesa"Around quotes
i(Inside parensa(Around parens
itInside tagatAround tag
ipInner paragraphapAround paragraph

Operators + Motion = Power

  • dw - Delete word
  • ci" - Change inside quotes
  • yap - Yank around paragraph
  • >i{ - Indent inside braces
  • gUiw - Uppercase inner word

References