NeoVIM
- advanced vim topics tips and tricks
- learn vim progressively
- quickfix and location lists in vim
- stop using vim like a pig
- vim fugitive in action
- vim mode tips
Getting Help
:h <search>- Get help on any topic:h ^n- SearchCTRL-Npurpose in normal mode:h i_^n- SearchCTRL-Npurpose 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 leftj- Move downk- Move upl- Move right
Word Movement
w- Start of next worde- End of current wordb- Beginning of current/previous wordW,E,B- Same as above but skip punctuation
Line Movement
0- Beginning of line^- First non-whitespace character$- End of lineg_- Last non-whitespace character
Screen Movement
H- Top of screenM- Middle of screenL- Bottom of screen<C-u>- Page up<C-d>- Page down<C-b>- Full page up<C-f>- Full page down
File Movement
ggorgo- First line of fileG- 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 cursorn- Next search resultN- Previous search result
Advanced Line Navigation
f<char>- Find character on current lineF<char>- Find character backwards on current linet<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 linegf- Open file under cursorgv- Re-select previous visual selectiongo- Go to byte offset (neovim only)gi- Insert text in the same position as where Insert mode was stopped last time in the current bufferg;- Go to older position in change listg,- Go to newer position in change listg&- 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 cursora- Insert after cursorI- Insert at beginning of lineA- Insert at end of lineo- Open new line belowO- Open new line aboves- Substitute character (delete and insert)S- Substitute line (delete line and insert)
Deletion
x- Delete character under cursorX- Delete character before cursordw- Delete worddd- Delete lineD- Delete to end of lined$- Delete to end of line (same as D)d0- Delete to beginning of lines/\s\+$//- delete all trailing spaces in empty lines
Change (Delete and Insert)
cw- Change wordcc- Change lineC- Change to end of linec$- Change to end of line (same as C)r<char>- Replace single characterR- Replace mode (overwrite)
Copy (Yank) and Paste
yw- Yank wordyy- Yank lineY- Yank to end of linep- Paste after cursor/lineP- Paste before cursor/line
Undo and Redo
u- Undo last change<C-r>- RedoU- Undo all changes on current line
Repeat
.- Repeat last change@@- Repeat last macro
Search and Replace
Basic Search
/pattern- Search forward?pattern- Search backwardn- Next matchN- 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 commandg&- 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- Words- Sentencep- Paragrapht- Tag (HTML/XML)"'`- Quoted strings()[]{}<>- Bracketed text
Examples
diw- Delete inner wordcis- Change inner sentenceyap- Yank around paragraphvi"- Select inner quoted textda(- 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 macroQ- Repeat last macro10@a- Execute macro 10 timesqA- Append actions to the macro ‘a’
Visual Mode Mastery
Visual Mode Types
v- Character-wise visual modeV- Line-wise visual mode<C-v>- Block-wise visual mode
Visual Mode Operations
o- Move to opposite end of selectionO- 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 wordz=— Suggestions for correctionzg— Add word to dictionaryzw— 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 spacezz- Center current line on screenzt- Move current line to top of screenzb- 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 lineD- Delete to end of linecc- Change entire linedd- Delete entire line
Text Transformation
gq- Format/wrap long linesgu- Convert to lowercasegU- Convert to uppercaseg~- Toggle case</>- Indent/Un-indent (in visual mode)==- Auto-indent current linegg=G- Auto-indent entire file
Advanced Movement Patterns
f<char>→;- Find character and repeatt<char>→,- Till character and reverseci"- Change inside quotesda(- 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 withfg):mksession session.vim- Save session:source session.vim- Load sessionvim -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 foldzo- Open foldzc- Close foldzR- Open all foldszM- Close all folds
Diffing
:windo diffthis- Compare two windowsnvim -d file1 file2- Start in diff mode]c/[c- Next/previous diffdo- 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 cursorga- Show ASCII value of characterg8- 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
| Command | Action | Command | Action |
|---|---|---|---|
i | Insert mode | <Esc> | Normal mode |
h j k l | Move cursor | w b e | Word movement |
x | Delete char | dd | Delete line |
yy | Copy line | p | Paste |
u | Undo | <C-r> | Redo |
:w | Save | :q | Quit |
/text | Search | n | Next match |
* | Find word | cgn | Change match |
. | Repeat | % | Match bracket |
Text Objects (use with d, c, y, v)
| Object | Description | Object | Description |
|---|---|---|---|
iw | Inner word | aw | Around word |
i" | Inside quotes | a" | Around quotes |
i( | Inside parens | a( | Around parens |
it | Inside tag | at | Around tag |
ip | Inner paragraph | ap | Around paragraph |
Operators + Motion = Power
dw- Delete wordci"- Change inside quotesyap- Yank around paragraph>i{- Indent inside bracesgUiw- Uppercase inner word
References
:help tutor- Built-in interactive tutorial- Vim Adventures - Gamified learning
- OpenVim - Interactive tutorial
- Practical Vim - Essential book
- Vim Documentation - Official docs
- Vim Wiki - Community tips
- Vim ZZ and Vim zz | Do you know the difference?