Vim

From Things and Stuff Wiki
Revision as of 18:41, 31 December 2011 by Milk (talk | contribs) (GNAR)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Quick

Guides

Video

Main stuff

:w - save
:w filename - save as
:q - quit if saved
:x - save if changed and quit
ZZ - save as above
ZQ - even if not saved (also :q!)

:ex . - explore files in file directory. opens in split pans if file modified.
:Sex - as above but forces split
:Vex - as above but vertical split
:Tex - as above but in new tab
  <enter> - open file
  o - open file in new split buffer

Moving around

zz - centre screen on cursor
zt - move screen top to cursor
zb - move screen bottom to cursor
0 - line beginning
^ - first non-whitespace character
$ - line end
e - forward word
E - forward WORD
b - back word
B - back WORD
{ - beginning of previous
} - next paragraph
( - beginning of previous
) - next sentence
% - current brace

/ - find text
* - jump to next occurrence of work
n - repeat forwards
N - repeat backwards
H - cursor to top of screen (high)
M - cursor to middle of screen (middle)
L - cursor to bottom of screen (low)
Ctrl-D  move cursor down half-page
Ctrl-U  move cursor up half-page
gg - start of file
G - end of file
123G - move to line 123
ma - mark cursor position 'a'
'a - move to mark position 'a'

Operations

. - repeat last change
u - undo last change
U - undo all changes to current line
C-r - redo
i - insert at cursor
I - insert at line beginning

a - append after the cursor
A - append at the end of the line

o - add ('open') line below and insert
O - add line above and insert
x - delete character under cursor
X - delete character before cursor
y - yank (copy) current text
  yy - yank current line
p - paste yanked text after cursor/line
P - pasted yanked text before cursor/line

d - delete and yank (cut)
  dl - delete character (alias: "x")
  dd - delete current line including linebreak
  dw - delete to end of word from cursor
  d$ - delete to end of line from cursor
  diw - delete inner word
  diW - delete inner WORD
  daw - delete word, including a space
  daW - delete WORD, including space
  dis -   delete inner sentence
  das -  delete a sentence
  dib -  delete inner '(' ')' block
  dab -  delete a '(' ')' block
  dip -  delete inner paragraph
  dap -  delete a paragraph
  diB -  delete inner '{' '}' block
  daB -  delete a '{' '}' block

c - change (delete and insert)
  cc - delete current line including linebreak, insert
  cw - delete to end of word from cursor, insert
  c$ - delete to end of line from cursor, insert
  ciw - delete inner word, insert
  ci" - change inner quoted string
  ci( - change inner brackets
  ci[ - change inner contents of [].. ci], ci) for insert on closing bracket
  caw - change an object
  etc.

v - visual select text
  viw - visual inner word
  viw~ - visual inner word, toggle case
  vip - visual inner paragraph
  vec - visual, end of word, change highlighted
V - visual select lines
Ctrl-v - visual select a block

s - change one character and insert

Ctrl-Y - copy character from line above
Ctrl-E - copy character from line below

Ctrl-N - auto-completion next match
Ctrl-P - auto-completion previous match
 
> - indent right
< - indent left
:s/foo/bar/ - search and replace first occurrence
:s/foo/bar/s - search and replace, global current line
:%s/foo/bar/g - search and replace, global whole file
:%s/foo/bar/gc - search and replace, with confirm

:g/text string/d - delete all lines with text string
:!g/text string/d - delete all lines without text string
qa - start recording macro 'a', q - stop recording
qA - start appending to macro 'a'
@a - play macro a
@@ - execute again
3@a - play macro 3 thrice

:let @a='macrogoeshere' - write macro manually
Ctrl-R Ctrl-R a - insert mode
:let @a='Ctrl-R Ctrl-R a - edit existing macro
[edit] Fugitive.vim

:Gwrite - git add file
:Gread - git checkout (revert) open to staged version
:Gremove - git rm and close buffer
:Gmove - git mv file. with /, relative to git root; without, relative to file
:Gcommit - git commit, opens message buffer
:Gblame - open split window with git blame details
:Gbrowse - open Github, else git instaweb for local sevrer

vimdiff resolution:
:diffget [buffer] - get diff from another buffer
:diffput [buffer] - put diff into another buffer
:diffupdate - update diff colouring
dg - get from other buffer pane (only 2 pane)
dp - put to other buffer pane (works in 3 pane)
[c - jump to previous changeset
]c - jump to next changeset
:only - close buffers other than active 

Split windows

:sp [filename]
Ctrl-W - move up window
Ctrl-W x - switch windows

Folds

zo - open fold
zO - open fold recursively
zc - close fold
zC - close fold resursive
zR - open all folds
zM - close all
zj - go down and up a fold
zk - go up a fold

Handy

:e scp://root@example.com/~user/folder/.config - open remote file
$ vim scp://root@example.com/~user/folder/.config
:reg - list register contents
K - in normal mode, run man for current word (opens "man word" in shell)

Vimscripts

cmdalias.vim : Create aliases for Vim commands. [1]