Skip to content

Instantly share code, notes, and snippets.

@amandarae
Forked from zefer/vim_cheatsheet.md
Created August 15, 2012 21:39
Show Gist options
  • Save amandarae/3363930 to your computer and use it in GitHub Desktop.
Save amandarae/3363930 to your computer and use it in GitHub Desktop.
My newbie Vim cheatsheet

#Vim Cheat Sheet by jessedearing

  • gqip - Reformats paragraph to textwidth

  • gq - Reformats selection

  • :Tab /= - Equally spaces based on the = symbol (requires Tabular vim plugin)

  • :setf language - Changes current language

  • :set language=language - Changes current language

  • <C-a> - Increments the number under the cursor

  • <C-x> - Decrements the number under the cursor

  • ~ - Toggles case and moves to next character in normal mode

  • gU<movement> - Uppercases

  • gu<movement> - Lowercases

  • <visual>U - Uppercases in Visual mode

  • <visual>u - Uppercases in Visual mode

  • <C-o> - While in insert mode lets you run a single command-mode command and returns you back to insert mode ##Sed-style Replacements

  • s/<sed regex>/<replacement>/<flags>

    • <flags> - Can be a combintion of g for global (all occurances in a line, i to ignore case, and c to confirm each substitution

###Sed-style Regex

  • \? - 0 or 1
  • * - 0 or more
  • \+ - Matches one or more
  • \( \) - Captured grouping (can be used later in replacement with \1, \2, etc.
  • \%( \) - Non capturing group
  • ^ and $ - Beginning and end of line respectively

##Surround plugin

  • ys<motion or text><char to wrap> - Surrounds the text in the motion (Example: w for to the next word or $ to the end of the line) with the character, HTML tag, etc.
  • cs<old char><new char> - Changes the surrounding character
  • cst<html tag> - Changes an html tag from the current to a new one
  • ctrl-s - Adds a surrounding in insert mode

##Fugitive (Git extensions)

  • :Gdiff - Diffs file with current version in repository
  • :Gw - Saves and git adds
  • :Gco or :Gcommit - Commits

Three Way Diff

3 way reconciliation

##Ack.vim

o    to open (same as enter)
go   to preview file (open but maintain focus on ack.vim results)
t    to open in new tab
T    to open in new tab silently
v    to open in vertical split
gv   to open in vertical split silently
q    to close the quickfix window

Enter insert mode

i    insert before character under cursor
a    insert after cursor
I    insert at beginning of current line
A    insert at end of the line
o    starts insert mode in a new line below current one
O    insert in a new line above current one

Enter insert mode & replace

ciw         ("change inner word") change word under cursor
ci"         change double-quoted string (but keep the quotes)
ci(         change text between matching parentheses, also works with brackets
cc          change whole line
c/}<ent>    change from cursor to next occurrence of }
c3/end<ent> change from cursor to third occurrence of end
cw          change word
c3w         change 3 words

Find & replace

replace on line     :s/<find>/<replace>
replace globally    :%s/<find>/<replace>//gc

Find in files

:vim /something/gj **/*.rb   search for 'something' in all subdirs. j=don't jump to first result
:cw                          open the results in a 'quick fix list'
:lvim /something/gj **/*.rb  (as above but local to current window)
:lw                          (as above but local to current window)

Move

  k
h   l     Cursor movement
  j

→    ←
/    ?    search for a pattern of text, jump to it by hitting Enter (<CR>)
*    #    search for the word under cursor
n    N    jump to the next match for the previous search
$    ^    position cursor at end of current line
f    F    position cursor on the character in the same line that matches the next keystroke
t    T    position cursor before the next character that matches the keystroke
;    ,    repeat the last f, F, t, or T
w    b    move to start of next word
W    B    move to start of next "WORD" (sequence of non-blank characters)
}    {    move down one paragraph (block of text separated by blank lines)
gg        jump to first line of document
G         jump to end of document
22G       go to line 22
:22       go to line 22

Manipulating text

cut the current line                  dd
copy the current line                 yy
paste below current line              p
paste above current line              P
remove the character under cursor     x
remove the character before cursor    X
delete the word under cursor          de
delete to the end of the line         d$

remove five lines starting here       5dd
copy five lines starting here         5yy 

indent this line                      >>
indent five lines starting here       5>>

replace mode (overtype)               r
replace with x                        rx

Copy & paste registers

copy & paste using default register   yy      p
copy & paste using register 'a'       "ayy    "ap
copy & paste using system clipboard   "+yy    "+p
copy & paste using system clipboard   "*yy    "*p

Auto complete/suggest C-p

Visual mode selection

Visual mode         v
Visual Line mode    V
Visual Block mode   <CTRL>v

Split panes

:split    new horizontal pane
:vsplit   new vertical pane
<C-w>     move next
<C-k>     move up
<C-j>     move down
<C-h>     move left
<C-l>     move right
<C-w> x   swap panes
<C-w> |   max width
<C-w> _   max height
<C-w> =   equalise sizes

Command-T

<C-CR>    open the selected file in a new split window
<C-s>     open the selected file in a new split window
<C-v>     open the selected file in a new vertical split window
<C-t>     open the selected file in a new tab

Commands

Run a command   !<command>
Open a shell    :sh

Resources

vim-revisited

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment