Chapter 5: Text, editors and the shell environment

grep, vim and Bash cheat sheet

The chapter on one page: what to remember, the operators, patterns and keys to have at your fingertips, and flashcards for quick revision.

The chapter in eight sentences

Cheat sheet

Redirection

WriteMeans
cmd > file, cmd >> filestdout to a file: replace, append
cmd 2> file, cmd 2>> filestderr to a file
cmd &> file (> file 2>&1)both to one file
cmd 2> /dev/nulldiscard errors
cmd < filestdin from a file
cmd1 | cmd2stdout of cmd1 into stdin of cmd2
cmd | tee file | cmd2save a copy and pass it on (-a appends)

grep

WriteMeans
grep -i, -v, -n, -cignore case, invert, number lines, count
grep -w, -r, -l, -Ewhole words, recurse, file names only, extended
^, $, ^$start, end, empty line
., .*, [abc], [^abc], [[:digit:]]any char, anything, set, not in set, class
\<, \>start, end of a word
-E: + ? {n,m} a|b ( )one or more, optional, count, either, group

vim

KeysDo
i a o, Escinsert before / after / new line, back to normal
:w, :wq, :q!save, save and quit, quit discarding
x, dd, yy, pdelete char, cut line, copy line, paste
u, Ctrl+Rundo, redo
gg, G, :Nfirst line, last line, line N
/text, nsearch, next match
:%s/old/new/greplace everywhere
v, V, Ctrl+Vselect characters, lines, block

Environment

WriteMeans
NAME=value, $NAME, ${NAME}set, read, read next to other text
export NAME, export NAME=valuemake it an environment variable
unset NAME, env, setremove, list environment, list all
NAME=value cmdset it for one command
export PATH=$PATH:/diradd a directory to the command search
alias name='cmd', unalias namecreate, remove an alias
~/.bashrc, ~/.bash_profile, /etc/profile.d/*.shpersonal per shell, personal per login, everyone
source ~/.bashrcapply to the current shell

Flashcards

The three standard streams and their numbers?
stdin **0**, stdout **1**, stderr **2**.
Append output to a file instead of replacing it?
`>>`
Hide a command's error messages?
`cmd 2> /dev/null`
Save a pipeline's data to a file and keep it flowing?
`… | tee file | …`
Show lines that do not match a pattern?
`grep -v PATTERN`
What do ^ and $ mean in a regular expression?
The start and the end of the line.
What does .* match?
Any characters, any number of times, including none.
Which grep option makes +, ?, | and ( ) work without backslashes?
`-E` (extended regular expressions)
vim is doing odd things when you type. First move?
Press **Esc** to get back to normal mode.
Quit vim and discard your changes?
`:q!`
Copy a line and paste it below in vim?
`yy` then `p`
Make a variable visible to programs you start?
`export NAME`
Where do personal aliases go?
`~/.bashrc`
Apply an edited ~/.bashrc to the current shell?
`source ~/.bashrc`