Skip to content

Instantly share code, notes, and snippets.

@fallwith
Last active May 18, 2018 03:56
Show Gist options
  • Save fallwith/9769300 to your computer and use it in GitHub Desktop.
Save fallwith/9769300 to your computer and use it in GitHub Desktop.
Fallwith's Vim Guide

Vim Guide

2014-2018 by fallwith

Legend

sequence meaning
<C-a> Ctrl + a
<S-v> Shift + v

Built-in Functionality

Repetition

input result
. repeat the last edit command
@: repeat the last normal mode command
@@ repeat the previously repeated command

Commands

input result
:ls list all open files
:changes list all changes made to the current file
:map display all defined key mappings
:X save and encrypt the current file
:!{command} %:p send the full path of the current file to {command}
:r {source} read from source (file or command) and put the contents at the current cursor location
:sort sort the selected rows
:next go to the next open file
:previous go to the previous open file
:help license view information about Vim's charityware license

Command Line Options

input result
vim +{num} {file} open the file to the given line number
vim + {file} open the file to the end

Opening Files

input result
:e {filename} open the given file for editing
:pwd print the current working directory
:e %:h start the file path with the expansion of the full path of the current file's directory
:find {filename} search for the given file beneath known search paths
:set path+={dir}/** add {dir} and its subdirectories to the search path
:e. open the netrw file explorer for the current working directory
:Explore open the netrw file explorer for the current buffer's directory
:Sexplore same as :Explore, using a horizontal split
:Vexplore same as :Explore, using a vertical split
% hit % when in a netrw explorer window to create a new empty file
R hit R when in a netrw explorer window to rename a file
D hit D when in a netrw explorer window to delete a file
<C-w> or :new create a horizontal split window with a new unnamed buffer for editing
:enew open a new unnamed buffer for editing within the current window
:vnew create a vertical split window with a new unnamed buffer for editing
:tabnew create a new tab with a new unnamed buffer for editing

Search

input result
/{pattern} search forwards for pattern
?{pattern} search backwards for pattern
n repeat search in forwards direction
N repeat search in backwards direction
% find matching brace/bracket/parens
* search for the next occurrence of the word under the cursor
# search for the previous occurrence of the word under the cursor
f{char} go to the next occurrence of {char}, with the cursor placed on {char}
F{char} go to the previous occurrence of {char}, with the cursor placed on {char}
t{char} go to the next occurrence of {char}, with the cursor placed to the left of {char}
T{char} go to the previous occurrence of {char}, with the cursor placed to the right of {char}
; repeat the previous f,F,t, or T search in the same direction
, repeat the previous f,F,t, or T search in the opposite direction
[I list all occurrences of the word under the cursor
:%s/{from}/{to}/g replace all instances of {from} with {to} across the entire buffer

Windows and Splits

input result
<C-w> w cycle forwards through available windows
<C-w> W cycle backwards through available windows
<C-w> h change focus to the window to the left
<C-w> l change focus to the window to the right
<C-w> j change focus to the window below
<C-w> k change focus to the window above
<C-w> o or :only make the current window be the only window
<C-w> c or :close close the active window
<C-w> = make the width and heigh of all windows equal
<C-w> _ maximize the height of the current window
<C-w> | maximize the width of the current window
{num} <C-w> _ set the window's height to {num} rows
{num} <C-w> | set the window's width to {num} columns
:below new create a buffer in a new horizontal split
:below vnew create a buffer in a new vertical split
{num} <C-w> + increase the height of a horizontal split by the given number of rows
{num} <C-w> - decrease the height of a horizontal split by the given number of rows
{num} <C-w> > increase the width of a vertical split by the given number of columns
{num} <C-w> < decrease the width of a vertical split by the given number of columns

Folds

input result
za toggle fold (closes if open, opens if closed)
zc close fold
zo open fold
zA toggle all folds at cursor
zC close all folds at cursor
zO open all folds at cursor
zr open one more level of fold throughout the entire buffer
zR open all folds in buffer

Buffers

input result
:b{num} jump to the buffer with the given number
:bnext jump to the next buffer on the list
:bprevious jump to the previous buffer on the list
<C-^> jump to the most recently used previous buffer
{num} <C-^> jump to the buffer with the given buffer
:bfirst jump to the first buffer
:blast jump to the last buffer
:ls list buffers
:bw wipe (close) buffer

Tabs

input result
:tabedit {filename} (or :tabe) open the given file in a new tab
:tabs list all open tabs
:tabclose (or :tabc) close the current tab
:tabonly (or :tabo) close all tabs except the current one
:tabdo {command} execute the given command across all tabs
:tabnext or gt (or :tabn) go to the next tab
:tabnext {num} (or {num} gt) (or :tabn {num}) go to tab n
:tabprevious or gT (or :tabp) go to the previous tab
:tabmove {num} move the tab {num} positions (use 0 to move a tab to the beginning of the list)
:tabmove move the tab to the end of the list

Movement

input result
w go forwards to the start of the next word
W go forwards to the start of the next word, ignoring punctuation
b go back to the start of the word, or to the previous word if already at the start
B go back to the start of the word, or to the previous word, ignoring punctuation
0 go to the start of the line (first character)
^ go to the start of the line (first non-blank character)
$ go to the end of the line
- go to the start of the previous line
<C-u> half page up
<C-d> half page down
<C-b> full page up
<C-f> full page down
<C-y> line up
<C-e> line down
H move to the first line of the current window
M move to the middle line of the current window
L move to the last line of the current window
) move to beginning of next sentence
( move to beginning of current sentence
} move to beginning of next paragraph
{ move to beginning of the current paragraph
e move to end of word
E move to end of word, ignoring punctuation
ge move backwards to the end of the previous word
gE move backwards to the end of the previous word, ignoring punctuation
]] move to beginning of next section
[[ move to beginning of current section
f{char} find and move to next occurrence of char in line
F{char} find and move to previous occurrence of char in line
t{char} find and move to char before the next occurrence of the given char in line
T{char} find and move to char after previous occurrence of the given char in line
; repeat find command in same direction
, repeat find command in opposite direction
`` return to original position before issuing {num}G
G end of file
gg start of file
{num}G go to line number
:{num} go to line number
<C-o> go backwards through the jump list
<C-i> go forwards through the jump list

Inputting and Altering Text

input result
a append text after the cursor
A append text at the end of the line
c{motion} change chars
cc change current line
C alias for c$, change text from the cursor to the end of the line
d{motion} delete chars in the given direction
dd delete current line
D delete from cursor until end of line
J join the current line with the next (delete the current line's newline)
o begin a new line below the cursor and enter insert mode
O begin a new line above the cursor and enter insert mode
s substitute current char
S substitute current line
r replace char
R replace (overwrite) text across entire line
x delete current char
xp transpose the current char and the next
cib or ciB change the text inside parens / braces
{number}> indent by the given number of tabs (or just use '>' for one indent)
{number}< outdent the given number of tabs (or just use '<' for one outdent)
= autoindent text
<C-h> (insert mode) delete one character backwards (backspace)
<C-w> (insert mode) delete back one word
<C-u> (insert mode) delete back to the start of the line

Visual Selection

input result
v toggle character-wise visual mode
V toggle line-wise visual mode
<C-v> enable block-wise visual mode
gv enter visual mode and re-select the previous selection
o (when text is selected) go to the other end of the selected text
v{motion} select characters according to the given motion (vw, v$, etc.)
vi) select from cursor until the closing )
ggVG select all text

Text Objects

object definition
aw a word
iw inner word
aW a WORD
iW inner WORD
as a sentence
is inner sentence
ap a paragraph
ip inner paragraph
a] or a[ a [] block
i] or i[ inner [] block
a) or a( or ab a block
i) or i( or ib inner block
a> or a< a <> block
i> or i< inner <> block
at a tag block
it inner tag block
a} or a{ or aB a Block
i} or i{ or iB inner Block
a" or a' or a` a quoted string
i" or i' or i` a quoted string, excluding the quotes

Yank / Put / Registers

input result
yy yank current line (to first register)
y{motion} yank chars in the given direction
"{char}yy yank current line to register {char}
"{num}{char}yy yank next {num} lines to register {char}
"{char}P put contents of register {char} before cursor
"{char}p put contents of register {char} after cursor
"{char}{num}dd delete {num} lines into register {char}
"{char}d) delete from cursor to end of sentence into register {char}
*{capital char}y) add the next sentence to register {char} lower case = overwrite, upper case = append
:reg list the registers (most recent yanks)
<C-r>{register} (insert mode) put from the given register
<C-r>= (insert mode) bring up the expression register and then insert the output
"{num}p paste from the given yank register
"_dd delete to _, the black hole register (does not occupy a yank register)
"% name of the current file (use "%p to put the name)
": last Ex command (use ":p to put the command)
"/ last search pattern (use "/p to put the pattern)
". last inserted text (use ".p to put the text)
"# name of the alternate file (use "#p to put the name)
"= expression register. ex: "=2+2<CR>p will put '4'

Undo / Redo

input result
u undo last operation
U undo everything done to the current line
<C-r> (or :redo) redo last operation
:e! discard all changes made since the last write
g; go to where the previous change was made
g, go to where the next change was made
`. go to where the last change was made
`^ go to where the last insert was made

Macros

input result
q{char} record a macro
q stop recording
@{char} play back macro
@@ replay previously played macro

Marks

input result
m{letter} marks the current position with letter
'{letter} moves to the first character of the line marked by letter
`{letter} moves the cursor to the character marked by letter
`` returns to the position of the previous mark or context after a move
'' (2 apostrophes) returns to the beginning of the line of the previous mark or context
:marks show all marks
:delm {letter} delete mark
:delm! delete all marks
`" position last edited before exit
`[ go to first character of previously changed or yanked text
`] go to last character of previously changed or yanked text
'< go to first line of previously selected visual area
'> go to last line of previously selected visual area
'. go to the position where the last change was made
'^ go to the position where the cursor was the last time when insert mode was exited

Regex

input meaning
within a regex pattern
& entire text matched by the search pattern
~ shorthand for the previous replacement pattern used
\l or \u convert to lowercase or uppercase the next letter that follows
\L or \U convert to lowercase or uppercase all characters until \e or the end
\<foo\> word boundaries, match 'foo' but not 'fool'
%({pattern}) backred-less grouping (preface parens with % to make them non-capturing)
\zs sets the start of a match
\ze sets the end of a match
\c force case insensitivity
\C force case sensitivity
{-n,m} conservative quantifiers
{pattern}@= zero width match
{pattern}@! zero width non-match
{pattern}@<= zero width preceding match
{pattern}@<! zero width preceding non-match
{pattern}@> match without retry
within normal mode
:s or & repeat the last substitution
:%s/{from}/{to}/g globally search and replace
:%s/{from}/{to}/c request a confirmation prompt for each match prior to applying the substitution
:{num},{num}s/// perform a substitution within a range of lines
:1,10s/.*/(&)/ example: surround each line 1-10 with parens
/\v{pattern}/ enable very magic behavior (no need to escape special chars)
:h perl-patterns view a Vim regex / Perl regex compatibility chart

Spell Check

input result
:set spell enable spellcheck
:set nospell disable spellcheck
]s next misspelled word
[s previous misspelled word
z= correct spelling (from a menu)
zg teach Vim that the selected word's spelling is valid

Abbreviations

input result
:ab {abbreviation} {phrase} define an abbreviation that when entered will be replaced with the given phrase
{abbreviation}<Space> replace the given abbreviation with its defined phrase
{abbreviation}<C-[> replace the given abbreviation with its defined phrase
{abbreviation}<C-v> do not replace the given abbreviation and leave it as entered literally
:ab list all abbreviations
:ab {start of abbreviation} list all abbreviations that start with the given characters
:unab {abbreviation} undefine the given abbreviation
:norea {abbrev.} {phrase} define an abbreviation that cannot be remapped later
:abc undefine all abbreviations
:ca {abbreviation} {phrase} define an abbreviation that is only used in command-line mode
:cuna {abbreviation} undefine the given command-line mode abbreviation
:cnorea {abbrev.} {phrase} define a command-line mode abbreviation that cannot be remapped later
:cabc undefine all command-line mode abbreviations
:ia {abbreviation} {phrase} define an abbreviation that is only used in insert mode
:iuna {abbreviation} undefine the given insert mode abbreviation
:inorea {abbrev.} {phrase} define an insert mode abbreviation that cannot be remapped later
:iabc undefine all insert mode abbreviations

Jumps

input result
:jumps list all known jump locations
<C-o> jump back
<C-i> jump forwards
{num}<C-o> jump to the numbered jump appearing above 0 in the jump list
{num}<C-i> jump to the numbered jump appearing below 0 in the jump list

Changing Case

input result
u convert selected text to lower case
U convert selected text to upper case
~ toggle case of selected text / current char (and move cursor forward for char)
{num}~ toggle case of the next n characters
g~{motion} toggle case of characters from the current positon through the given motion
guu convert entire line to lower case
gUU convert entire line to upper case
g~~ toggle case of the entire line
guaw convert word to lower case
gUaw convert word to upper case

Increment/Decrement

input result
<C-a> increment number
<C-x> decrement number

Working With The OS Clipboard

input result
<S-Ins> paste from clipboard when in insert mode
"*p paste from clipboard when in normal mode
:%y+ copy whole file to clipboard
:y+ copy the current line to the clipboard
:{num1},{num2}y+ copy the range of lines to the clipboard

Display Lines (Dealing With Long Wrapped Lines)

input result
gj move down a display line
gk move up a display line
g^ go to the start of a display line (first non-blank character)
g0 go to the start of a display line (first character)
g$ go to the end of a display line
gm go to the middle of a display line

Ctags

input result
:set tags=.tags;/ look for a ctags file at .tags, walking the directory tree upwards
<C-]> jump to tag
<C-t> jump back
:ta {function name} jump to function definition
:ts tag list
:tn go to the next tag
:tp go to the previous tag
:tf go to the first function
:tl go to the last function

Vimdiff

input result
vimdiff {file1} {file2} launch Vim to diff files 1 and 2 (alternatively use: vim -d {file1} {file2})
]c jump to the next change
[c jump to the previous change
:diffoff disable the diff view (use :diffoff! for all open files)
do get changes from other window into the current window
dp put the changes from current window into the other window

Miscellaneous

input result
gi return the cursor to the previous (in insert mode) position and enter insert mode
<C-n> autocomplete word (hit continuously to cycle through the available completions)
<C-g> status info about the current file
g <C-g> advanced file details
zz center cursor
ZZ write the file and quit Vim
<C-o> enter normal mode for one command, then jump back to insert mode when done
<C-[> switch from insert mode to normal mode
{num} % position the cursor at the given percentage of the way through the current buffer

Help

input result
:help access the built in help documentation
:help {subject} access help documentation for a specific subject (:help text-objects)
<C-]> follow the current hyperlink that the cursor is over

Easter Eggs

input result
:help 42 Douglas Adams related
:help holy-grail Monty Python related
:help! Hitchhiker's Guide related
:Ni! Monty Python related
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment