vi
TYPING TEXT
a Switch to insert mode after cursor
i Switch to insert mode before cursor
o Insert line below and switch to mode
O Insert line above and switch to mode
:r file Insert file after current line
cw/cc/c$ Clear word/line/end of line
R Switch to replace (overwrite) mode
C-h/BkSp Delete back character in insert mode
C-w Delete back word in insert mode
C-u Delete back to beginning of insert mode
Esc Switch out of insert/replace/etc. modes
rx Replace character with x
GENERAL
. Repeat last change
v Visual selection mode (use for cut/copy/paste)
:w name Save to file name
:wq Save current file and quit
:q! Quit without saving changes
:sh Execute shell commands
u/U Undo last change / all changes on line
:e name Edit file name
Use to share buffers/copy/paste between files
Specifying directory will bring up netrw
:C-w s
Horizontal split
:Vsp or C-w v
Vertical split
:Ex, :Vex, :Sex, :Rex
Explorer in current, vertical split, horizontal
split, and to return to explorer from file
NAVIGATION
C-d Scroll down (half a screen)
C-u Scroll up (half a screen)
C-f Page forward
C-b Page backward
C-g Show line count and percentage
G Go to last line
nG Go to line n
:n Go to line n
H Go to upper left corner
M Go to middle line
L Go to lower left corner
^ Go to beginning of line
$ Go to end of line
w Go forward one word
b Go back one word
fx Find x
; Repeat last find
DELETION (CUT)
dd Delete current line to general buffer
ndd Delete n lines to general buffer
dw Delete word to general buffer
dnw Delete n words to general buffer
d) Delete to end of sentence to general buffer
db Delete previous word to general buffer
D Delete to end of line to general buffer
x Delete character to general buffer
J Join lines
"xndd Delete n lines to buffer x
"Xndd Delete n lines; append to buffer x
COPY/PASTE
yy/Y Yank (copy) line to general buffer
yw Yank word to general buffer
"xnyy Yank n lines to buffer x
p/P Paste/put general buffer after/before cursor
"xp/P Paste/put text from buffer x after/before cursor
SEARCH AND REPLACE
/string Search forward
?string Search backward
n Repeat search
N Repeat search reverse
:addrs/old/new/ Replace first occurrence
:addrs/old/new/g Replace all occurrences
Address . Current line
n Line number n
.+n Current line plus n lines
$ Last line
/text/ Line that contains text
% Entire file
addr1,addr2 Specifies a range
Regex for search string ^ Beginning of line
$ End of line
. Any single character
* Any previous character
.* Any character
OPTIONS
:set option
:set nooption
list Invisible characters
number Line numbers
autoindent Indent after CR
showmatch Matching parentheses
showmode Mode on last line of screen
:set all Show values of all possible parameters
tmux
COMMAND MODE
C-b : Enter command mode
PANES
C-b ; Switch to last active
C-b % Split horizontally
C-b " Split vertically
C-b { Move current to left
C-b } Move current to right
C-b arrows Switch to pane in direction
C-b o Next pane
C-b q Show pane numbers
C-b q 0-9 Switch to pane by number
C-b z Expand pane on/off
C-b C-arrows Resize pane
C-b ! Convert to window
C-b x Close
:setw synchronize-panes Toggle synchronize-panes
WINDOWS
tmux new -s sess -n win,
:new -s sess -n win
Start new session sess with window win
C-b c Create window
C-b , Rename current window
C-b & Close current window
C-b p Previous window
C-b n Next window
C-b 0-9 Switch to window by number
C-b l Switch to last active
:swap-window -s srcnum -t targetnum>
Swap windows by number (current if -s skipped)
SESSIONS
tmux, tmux new, :new
Start new session
tmux new -s name, :new -s name
Start new session called name
tmux kill-session -t name
Kill session name
tmux kill-session -a
Kill all sessions but the current
tmux kill-session -a -t name
Kill all session but name
tmux a, :attach, :attach -t name
Attach to last session or name
C-b $ Rename session
C-b d Detach from session
:attach -d Detach others on session
C-b s, tmux ls Show all sessions
C-b w Session and Window Preview
C-b ( Move to previous session
C-b ) Move to next session
COPY MODE
:setw -g mode-keys vi Use vi keys in buffer
C-b [ Enter copy mode
C-b PgUp Enter copy mode and scroll one page up
q Quit copy mode
g Go to top line
G Go to bottom line
Arrows Scroll
w Move forward one word
b Move back one word
/, ? Search forward, backward
n, N Repeat search (next, previous)
Spacebar Start selection
Esc Clear selection
Enter Copy selection
C-b ] Paste selection (buffer 0)
:show-buffer Display buffer 0 contents
:capture-pane Copy visible contents of pane to a buffer
:list-buffers Show all buffers
:choose-buffer Show all buffers and paste selected
:save-buffer name Save buffer contents to file name
:delete-buffer -b n, :delete buffer_n
Delete buffer n content
MISC
:set -g option Set option for all sessions
:setw -g option Set option for all windows
:info Show all sessions, windows, panes
regex
EXPRESSIONS
. Any character except newline
[xyz] Either one of x, y, z
[^xyz] Any character except x, y, z
[x-y] Any character between x and y (can be a-z, A-Z, 0-9)
Combine as e.g. [a-ZA-Z0-9] for alphanumeric
() Define group e.g. (\w*)
\n Recall group with number
(?<name>) Define named group (.NET only)
\k<name> Recall group with name (.NET only)
| OR between expressions
\ Escape keywords
\b Backspace
\t Tab
\r, \n CR, LF
\e Escape
\nnn Octal (e.g. \040)
\xnn Hexadecimal (e.g. \x20)
\unnnn Unicode (e.g. \u0020)
\w Any word character
\W Any non-word character
\s Whitespace
\S Non-whitespace
\d Digit
\D Non-digit
SUFFIXES
? Zero or one times
* Zero or many times
+ One or more times
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times
ANCHORS
^ Beginning of line
$ End of line
\< Beginning of word
\> End of word
\b Beginning or end of word
\B Reverse of \b
\A Beginning of string
\Z End of string or line
\z End of string
\G End of previous match
grep
OPTIONS
egrep, grep -E
Enable extended regex
-i Ignore case
-v Don't match pattern
-w Only whole words
-c Count of matching (or non-matching with -v) lines
-l Print name of each file with match
-n Print line number before matching line
-r Recursive (subdirectories)
sed
SCRIPT =
PATTERN
/pattern/
Text or basic regex to match (use -r to specify extended regex)
n
Line number (e.g. "3a something" appends "something" after 3rd line)
/pattern1/,/pattern2/ or linenum1,linenum2
Between two patterns or lines
pattern~n
Skip by n lines (e.g. 3~2, /something~3)
OPTIONS
-n Disable printing of pattern space
-e script, -f script-file
Add script (or file) to execution list
-i Edit in place (to make backup, -i suffix)
-r Enable extended Regex
-s Treat files as separate
FIND AND REPLACE
s/old/new Replace first occurrence
s/old/new/g Replace all occurrences
/pattern/s/old/new Replace matching pattern
Use -r to specify regex for "old"
Use -e to apply multiple find-replaces
COMMANDS
a/i text Append (a) or insert (i) text after/before line
c text Replace line with text
d, D Delete all pattern space (d) or until newline (D)
e Treat pattern space as command, execute and replace
e cmd Execute command
F Print file name
p, P Print all pattern space (p) or until newline (P)
w file Write all pattern space (w) to file or until newline (W)
y/src/dst/ Transliterate characters in src with those in dst
z Empty (zap) pattern space
{ cmd ; cmd ... }
Group several commands together
= Print current input line number
awk
awk options program Run program on file or stream
OPTIONS
-F delimeter Use a non-default delimiter, default is whitespace
PROGRAMS
'{print $n}'
Print the nth column on each line
'{/text/ code}'
Run on lines containing text
'{code} END {code}'
Run on each line and run at the end
E.g. '{s+=$1} END {print s}'
'{s+=$1;print $1} END {print s}'