Shell Cheatsheet

format

Format table    %!column -t
format python   !python -m json.tool

editing

<C-h><C-w><C-u>     Insert mode go back, works for bash
<C-o>               Insert Normal Mode(wtf?) 
db                  Delete a word backward:
10<C-a>             add number with count
xp                  Transpose next two characters
:t                  copy
:t$                 copy to end of file
gi                  go to last place text is inserted

window

<C-w>s   split
<C-w>v   vertical split
<C-w>c   close window
<C-w>o   keep only active window

buffer

// create new buffer
:new        new window
:enew       current window

file

find a file recursively under same directory
:e **/test/Suite.java

check current dir
    :pwd 

:h Head of the file name (the last component and any separators removed)
    % file path

get current dir's other files
    :edit %:h<Tab>

find file by setting search path
    find *file
    set path+=~/Dropbox/journal/**

write to nonexist dir
    :!mkdir -p %:h; write

go to file under cursor
    gf {file_name}

edit file under cursor in a new window
    <C-w><C-f>

put current file path
    "%p

read a file
    r file.txt

reigster

simply containers that hold text

addressing a register
    "{register}

yank word to the "a" register
    "ayiw

Registers:
    "0      yank register
    "a-z    named register
    "+      system clipboard
    "*      selection
    ":      last Ex command
    ".      last inserted text
    "/      last search pattern

Paste register in inserted mode:
    <C-r>0  yank register
    <C-r>"  unmaned register
?  search backward
/  search forward
:nohlsearch  disable highlight temporarily
<C-r><C-w>    auto-complete in search
/word/e  offest search
//e      reuse last search and put cursor at end

patterns

\c   search case insensitive
\C   search case sensitive
\v   very magic search, can use \x
\V   very no magic search
<word>  set word boundry

subsitute

Special Characters

  \r    carriage return
  \t    tab
  \1    first submatch
  \2    second submatch
  \0 or &    entire submatch
  \={Vim script}  evalute script use result as replacement   decouple search and substitute, search with last search 
  %s//[word]/g   repeat last search and keep flags
  :&&   short hand for entire file
  g&

global command

grep
    :g/re/p

global
    :g/re/{cmd}

reverse global (:vglobal)
    :v/re/{cmd}

yank all match line into register a.

Using uppercase A to append to register a for each line.
    :g/TODO/yank A

Append to end of file
    :g/TODO/t$

For each range of lines beginning with {start} 
and ending with {finish}, run {cmd}
    :g/{start}/ .,{finish} {cmd}

misc

repeat last command to visual selection
:'<,'>normal .

Vim’s grammar has just one more rule: when an operator command is invoked in duplicate, it acts upon the current line. 

More writing at jakehao.com