Skip to content

Instantly share code, notes, and snippets.

@klingtnet
Created November 7, 2016 11:05
Show Gist options
  • Save klingtnet/fa1a11514bb781b67d10d793e0bcce6d to your computer and use it in GitHub Desktop.
Save klingtnet/fa1a11514bb781b67d10d793e0bcce6d to your computer and use it in GitHub Desktop.
An overview of vim and a list of important commands with examples

vim notes

  • the following notes are derived from this awesome presentation
  • a modal text editor
  • modes:
    • normal (default on start)
    • insert
    • visual
    • command mode
  • don't use the arrow keys
  • i,I insert (begin of line)
  • a,A append (line end)

motions

  • cursor motions:
    • use h,j,k,l for right,down,up,left
    • 0, $ jump begin/end of line
  • word motions
    • w, b next/prev word beginning
    • e, eg next/prev word ending
  • character jump:
    • f<char>, F<char> find next/prev (on character)
    • t<char>, T<char> find next/prev (one character before)
  • other movement:
    • ), ( next/prev sentences
    • }, { next/prev paragraphs
  • all those previous commands can be prefixed by a number (counts), e.g. 3j jumps three lines down
  • :set relativenumber enables relative numbering based on the current line, this makes relative jump counts fairly easy

edits (verbs and nouns)

  • change/replace c character, c$ = C till end of line, cc line
    • replace a word cw
  • delete d, d$ = D till end of line, dd line
  • xC, XC delete Count characters after/before
  • copy y character, Y = yy line
    • map Y y$ to get consistent behaviour

modifiers (adjectives)

  • aw a word, iw inner word
  • as a sentence, iw inner sentence
  • ap a paragraph, ip inner paragraph
  • at a tag, it inner tag
    • <h1>change in tag</h1> to replace the heading: cit (change in tag)
  • modify inside iX, outside aX, where X is ", `, ', <, ...

  • . repeat the last edit
  • J join lines

window movement

  • C-f, C-b page up/down
  • C-d, C-u half page up/down
  • C-y, C-e line up/down
  • relative to cursor: zt (top), zz (center), zb (bottom)

cursor movement

  • move cursor inside window: H (top), M (mid), L (bottom)
  • jump to <number>gg top, <number>G bottom of the file

selection

  • v char. wise
  • V line wise
  • C-v block wise

command mode

  • : enter command mode
  • wq wqa write quite (all)
  • x, xa only write if changes were made
  • w! discard and quit
  • r <filepath> read-in file
  • %!command filter through external command
    • e.g. %!tr '[:lower:]' '[:upper:]'
  • :.!command insert command output in place
    • the . can be replaced with any position

search and replace

  • %s/foo/bar/g replace every occurence of foo with bar, g will match multiple occurences in one line
  • %s/foo/bar/gc like above with confirm

ranges

  • % = 1,$ entire file
  • .,$ from current to last line
  • X,/some text/ from line x to some text
    • e.g. v.,/autocomplete/ selects from current cursor to first occurence of autocomplete
  • /text/+1 line below text

search

  • / forward search
  • ? backward search

buffers/windows/tabs

  • a buffer is basically a file
  • :ls list buffers
  • :sp split window
  • a window is a view of a file
  • switch between windows C-{h,j,k,l}
  • a tab is an arrangement of windows (not like a usual tab in GUI text editors)

registers

  • copy/paste buffers are `registers
  • overview :reg
  • "0 last yanked
  • "1-"9 last deletes
  • named register "a-z
  • copy into named register "aY where a is the registers name and Y is yank line
  • GUI clipboards: "* and "+
  • yank the entire file/buffer into the system clipboard: %y+

general tips

  • :tab help start help in tab
  • :help <bs <tab>> autocomplete help
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment