├── bash-history-cheat-sheet.pdf ├── bash-history-cheat-sheet.tex ├── bash-history-cheat-sheet.txt └── readme.txt /bash-history-cheat-sheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkrumins/bash-history-cheat-sheet/d27271d56ea9db15e681ee79e521b97d6377123c/bash-history-cheat-sheet.pdf -------------------------------------------------------------------------------- /bash-history-cheat-sheet.tex: -------------------------------------------------------------------------------- 1 | % Bash History Cheat Sheet 2 | % 3 | % by Peteris Krumins (peter@catonmat.net) 4 | % http://www.catonmat.net - good coders code, great reuse 5 | % 6 | % 2008.02.13 7 | % 8 | 9 | \documentclass{article} 10 | 11 | \usepackage[left=1.5cm,top=1cm,right=1.5cm,bottom=1cm,nohead,nofoot]{geometry} 12 | 13 | \usepackage[pdftex]{hyperref} 14 | \usepackage{verbatim} 15 | \hypersetup{pdftitle={Bash History Cheat Sheet}} 16 | \hypersetup{pdfauthor={Peteris Krumins (peter@catonmat.net)}} 17 | \hypersetup{pdfkeywords={cheat sheet, bash, shell, bourne again shell, sh, history, command line, unix, linux, cheat sheat, cheet sheet, cheet sheat}} 18 | \hypersetup{pdfsubject={http://www.catonmat.net - good coders code, great reuse}} 19 | \hypersetup{colorlinks} 20 | 21 | \pagestyle{empty} 22 | 23 | % ----------------------------------------------------------------------- 24 | 25 | \begin{document} 26 | 27 | \begin{center} 28 | \Large Bash History Cheat Sheet 29 | \end{center} 30 | 31 | \vspace{0.4in} 32 | 33 | Emacs and Vi history editing keyboard shortcuts: 34 | 35 | \renewcommand{\arraystretch}{1.2} 36 | \begin{tabular}{|p{3.5cm}|p{14cm}|} 37 | \hline 38 | \large\textbf{Shortcut} & \large\textbf{Description} \\ 39 | \hline 40 | \multicolumn{2}{|l|}{\small\it{Emacs Mode Shortcuts:}} \\ 41 | \hline 42 | \textbf{CTRL-p} & Fetch the previous command from the history list. \\ 43 | \hline 44 | \textbf{CTRL-n} & Fetch the next command from the history list. \\ 45 | \hline 46 | \textbf{CTRL-r} & Search history backward (incremental search). \\ 47 | \hline 48 | \textbf{CTRL-s} & Search history forward (incremental search). \\ 49 | \hline 50 | \textbf{Meta-p} & Search backward using non-incremental search. \\ 51 | \hline 52 | \textbf{Meta-n} & Search forward using non-incremental search. \\ 53 | \hline 54 | \textbf{Meta-\textless} & Move to the first line in the history. \\ 55 | \hline 56 | \textbf{Meta-\textgreater} & Move to the end of the history list. \\ 57 | \hline 58 | \multicolumn{2}{|l|}{\small\it{Vi Mode Shortcuts:}} \\ 59 | \hline 60 | \textbf{k} & Fetch the previous command from the history list. \\ 61 | \hline 62 | \textbf{j} & Fetch the next command from the history list. \\ 63 | \hline 64 | \textbf{/\textit{string}} or \textbf{CTRL-r} & Search history backward for a command matching \textit{string}. \\ 65 | \hline 66 | \textbf{?\textit{string}} or \textbf{CTRL-s} & Search history forward for a command matching \textit{string}. \\ 67 | \hline 68 | \textbf{n} & Repeat search in the same direction as previous. \\ 69 | \hline 70 | \textbf{N} & Repeat search in the opposite direction as previous. \\ 71 | \hline 72 | \textbf{G} & Move to the N-th history line (for example, \textbf{15G}). \\ 73 | \hline 74 | \end{tabular} 75 | 76 | \vspace{0.4in} 77 | 78 | History behavior modification via shell variables: 79 | 80 | \begin{tabular}{|p{3.5cm}|p{14cm}|} 81 | \hline 82 | \large\textbf{Shell Variable} & \large\textbf{Description} \\ 83 | \hline 84 | \textbf{HISTFILE} & Controls where the history file gets saved. Set to \textbf{/dev/null} not to keep history.\newline Default: $\sim$/.bash\_history. \\ 85 | \hline 86 | \textbf{HISTFILESIZE} & Controls how many history commands to keep in \textbf{HISTFILE}.\newline Default: 500. \\ 87 | \hline 88 | \textbf{HISTSIZE} & Controls how many history commands to keep in the history list of current session.\newline Default: 500. \\ 89 | \hline 90 | \textbf{HISTIGNORE} & Controls which commands to ignore and not save to the history list. The variable takes a list of colon separated patterns. Pattern \textbf{\&} matches the previous history command. \\ 91 | \hline 92 | \end{tabular} 93 | 94 | \vspace{0.4in} 95 | 96 | History behavior modification via \textit{shopt} command: 97 | 98 | \begin{tabular}{|p{3.5cm}|p{14cm}|} 99 | \hline 100 | \large\textbf{shopt option} & \large\textbf{Description} \\ 101 | \hline 102 | \textbf{histappend} & Setting the variable appends current session history to \textbf{HISTFILE}. Unsetting overwrites the file each time. \\ 103 | \hline 104 | \textbf{histreedit} & If set, puts a failed history substitution back on the command line for re-editing. \\ 105 | \hline 106 | \textbf{histverify} & If set, puts the command to be executed after a substitution on command line as if you had typed it. \\ 107 | \hline 108 | \end{tabular} 109 | 110 | \vspace{0.2in} 111 | 112 | \textbf{shopt} options can be set by a \texttt{shopt -s option} and can be unset by a \texttt{shopt -u option} shell command. 113 | 114 | \vfill 115 | 116 | \framebox{\parbox{5in}{ 117 | A cheat sheet by \textbf{Peteris Krumins} (peter@catonmat.net), 2008. 118 | 119 | \href{http://www.catonmat.net}{http://www.catonmat.net} - good coders code, great reuse 120 | 121 | \vspace{2mm} 122 | \footnotesize{Released under GNU Free Document License.}}} 123 | 124 | %--------------------------------------------------------------------------- 125 | 126 | \newpage 127 | 128 | \mbox{} 129 | 130 | History expansion: 131 | 132 | \begin{tabular}{|p{3.5cm}|p{14cm}|} 133 | \hline 134 | \large\textbf{Shortcut} & \large\textbf{Description} \\ 135 | \hline 136 | \multicolumn{2}{|l|}{\small\it{Event Designators:}} \\ 137 | \hline 138 | \textbf{!} & Starts a history substitution. \\ 139 | \hline 140 | \textbf{!!} & Refers to the last command. \\ 141 | \hline 142 | \textbf{!n} & Refers to the \textbf{n}-th command line. \\ 143 | \hline 144 | \textbf{!-n} & Refers to the current command line minus \textbf{n}. \\ 145 | \hline 146 | \textbf{!string} & Refers to the most recent command starting with \textbf{string}. \\ 147 | \hline 148 | \textbf{!?string?} & Refers to the most recent command containing \textbf{string} (the ending ? is optional). \\ 149 | \hline 150 | \textbf{\^{}string1\^{}string2\^{}} & Quick substitution. Repeats the last command, replacing \textbf{string1} with \textbf{string2}. \\ 151 | \hline 152 | \textbf{!\#} & Refers to the entire command line typed so far. \\ 153 | \hline 154 | \multicolumn{2}{|l|}{\small\it{Word Designators (word designators follow the event designators, separated by a colon):}} \\ 155 | \hline 156 | \textbf{0} & The zeroth (first) word in a line (usually command name). \\ 157 | \hline 158 | \textbf{n} & The \textbf{n}-th word in a line. \\ 159 | \hline 160 | \textbf{\^{}} & The first argument (the second word) in a line. \\ 161 | \hline 162 | \textbf{\$} & The last argument in a line. \\ 163 | \hline 164 | \textbf{\%} & The word matched by the most recent ?string? search. \\ 165 | \hline 166 | \textbf{x-y} & A range of words from \textbf{x} to \textbf{y} (\textbf{-y} is synonymous with \textbf{0-y}). \\ 167 | \hline 168 | \textbf{*} & All word but the zeroth. \\ 169 | \hline 170 | \textbf{x*} & Synonymous with \textbf{x-\$}. \\ 171 | \hline 172 | \textbf{x-} & The words from \textbf{x} to the second to last word. \\ 173 | \hline 174 | \multicolumn{2}{|l|}{\small\it{Modifiers (modifiers follow word designators, separated by a colon):}} \\ 175 | \hline 176 | \textbf{h} & Removes a trailing pathname component, leaving the head. \\ 177 | \hline 178 | \textbf{t} & Removes all leading pathname components, leaving the tail. \\ 179 | \hline 180 | \textbf{r} & Removes a trailing suffix of the form .xxx, leaving the basename. \\ 181 | \hline 182 | \textbf{e} & Removes all but the trailing suffix. \\ 183 | \hline 184 | \textbf{p} & Prints the resulting command but does not execute it. \\ 185 | \hline 186 | \textbf{q} & Quotes the substituted words, escaping further substitutions. \\ 187 | \hline 188 | \textbf{x} & Quotes the substituted words, breaking them into words at blanks and newlines. \\ 189 | \hline 190 | \textbf{s/old/new/} & Substitutes \textbf{new} for \textbf{old}. \\ 191 | \hline 192 | \textbf{\&} & Repeats the previous substitution. \\ 193 | \hline 194 | \textbf{g} & Causes \textbf{s/old/new/} or \textbf{\&} to be applied over the entire event line. \\ 195 | \hline 196 | \end{tabular} 197 | 198 | \vspace{0.4in} 199 | History expansion examples: 200 | 201 | \begin{tabular}{p{10cm}|p{7.5cm}} 202 | \begin{verbatim} 203 | $ echo a b c d e (executes `echo ab c d e`) 204 | a b c d e 205 | $ echo !!:3-$ (executes `echo c d e`) 206 | c d e 207 | $ echo !-2:*:q (executes `echo 'a b c d e'`) 208 | a b c d e 209 | $ echo !-3:1:2:4:x (executes `echo 'a' 'b' 'd'`) 210 | a b d 211 | $ echo !-4:1-3:s/a/foo/:s/b/bar/:s/c/baz/ 212 | (executes `echo foo bar baz`) 213 | foo bar baz 214 | \end{verbatim} 215 | & 216 | \begin{verbatim} 217 | $ tar -xzf package-x.y.z.tgz 218 | ... 219 | $ cd !-1:$:r (exec's `cd package-x.y.z`) 220 | package-x.y.z $ 221 | 222 | $ ls -a /tmp 223 | file1 file2 file3 ... 224 | $ ^-a^-l^ (exec's `ls -l /tmp`) 225 | -rw------- 1 user user file1 226 | ... 227 | \end{verbatim} 228 | \end{tabular} 229 | 230 | \begin{comment} 231 | \texttt{\parbox{3.3cm}{% 232 | \$ echo a b c d e \\ 233 | a b c d e \\ 234 | \$ echo !!:3-\$ \\ 235 | c d e \\ 236 | \$ echo !-2:*:q \\ 237 | 'a b c d e' 238 | }} & 239 | \texttt{\parbox{4.8cm}{% 240 | \$ tar -xzf package-x.y.z.tgz \\ 241 | ... \\ 242 | \$ cd !-1:\$:r \\ 243 | package-x.y.z \$ 244 | }} & 245 | \texttt{\parbox{5.8cm}{% 246 | \$ ls -a /tmp \\ 247 | file1 file2 file3 ...\\ 248 | \$ \^{}-a\^{}-l\^{} \\ 249 | ls -l /tmp \\ 250 | -rw------- 1 user user file1 \\ 251 | ... 252 | }} \\ 253 | \end{comment} 254 | 255 | \vfill 256 | 257 | \framebox{\parbox{5in}{ 258 | A cheat sheet by \textbf{Peteris Krumins} (peter@catonmat.net), 2008. 259 | 260 | \href{http://www.catonmat.net}{http://www.catonmat.net} - good coders code, great reuse 261 | 262 | \vspace{2mm} 263 | \footnotesize{Released under GNU Free Document License.}}} 264 | 265 | \end{document} 266 | 267 | -------------------------------------------------------------------------------- /bash-history-cheat-sheet.txt: -------------------------------------------------------------------------------- 1 | .---------------------------------------------------------------------------. 2 | | | 3 | | Bash History Cheat Sheet | 4 | | | 5 | '---------------------------------------------------------------------------' 6 | | Peteris Krumins (peter@catonmat.net), 2008.02.15 | 7 | | http://www.catonmat.net - good coders code, great reuse | 8 | | | 9 | | Released under the GNU Free Document License | 10 | '---------------------------------------------------------------------------' 11 | 12 | ===================== Emacs Keyboard Shortcut Summary ===================== 13 | 14 | .--------------.------------------------------------------------------------. 15 | | | | 16 | | Shortcut | Description | 17 | | | | 18 | '--------------+------------------------------------------------------------' 19 | | C-p | Fetch the previous command from the history list. | 20 | '--------------+------------------------------------------------------------' 21 | | C-n | Fetch the next command from the history list. | 22 | '--------------+------------------------------------------------------------' 23 | | M-< | Move to the first line in the history. | 24 | '--------------+------------------------------------------------------------' 25 | | M-> | Move to the end of the input history. | 26 | '--------------+------------------------------------------------------------' 27 | | C-r | Search backward starting at the current line (incremental) | 28 | '--------------+------------------------------------------------------------' 29 | | C-s | Search forward starting at the current line (incremental). | 30 | '--------------+------------------------------------------------------------' 31 | | M-p | Search backward using non-incremental search. | 32 | '--------------+------------------------------------------------------------' 33 | | M-n | Search forward using non-incremental search | 34 | '--------------'------------------------------------------------------------' 35 | 36 | ======================= Vi Keyboard Shortcut Summary ====================== 37 | 38 | .--------------.------------------------------------------------------------. 39 | | | | 40 | | Shortcut | Description | 41 | | | | 42 | '--------------+------------------------------------------------------------' 43 | | k | Fetch the previous command from the history list. | 44 | '--------------+------------------------------------------------------------' 45 | | j | Fetch the next command from the history list. | 46 | '--------------+------------------------------------------------------------' 47 | | /string or | Search history backward for a command matching string. | 48 | | CTRL-r | | 49 | '--------------+------------------------------------------------------------' 50 | | ?string or | Search history forward for a command matching string. | 51 | | CTRL-s | (Note that on most machines Ctrl-s STOPS the terminal | 52 | | | output, change it with `stty' (Ctrl-q to resume)). | 53 | '--------------+------------------------------------------------------------' 54 | | n | Repeat search in the same direction as previous. | 55 | '--------------+------------------------------------------------------------' 56 | | N | Repeat search in the opposite direction as previous. | 57 | '--------------+------------------------------------------------------------' 58 | | G | Move to history line N (for example, 15G). | 59 | '--------------'------------------------------------------------------------' 60 | 61 | ======================== History Expansion Summary ======================== 62 | 63 | Event Designators: 64 | 65 | .--------------.------------------------------------------------------------. 66 | | | | 67 | | Designator | Description | 68 | | | | 69 | '--------------+------------------------------------------------------------' 70 | | ! | Start a history substitution. | 71 | '--------------+------------------------------------------------------------' 72 | | !! | Refer to the last command. | 73 | '--------------+------------------------------------------------------------' 74 | | !n | Refer to the n-th command line (try `history' command). | 75 | '--------------+------------------------------------------------------------' 76 | | !-n | Refer to the current command line minus n. | 77 | '--------------+------------------------------------------------------------' 78 | | !string | Refer to the most recent command starting with 'string'. | 79 | '--------------+------------------------------------------------------------' 80 | | !?string? | Refer to the most recent command containing 'string'. | 81 | '--------------+------------------------------------------------------------' 82 | | ^str1^str2^ | Quick substitution. Repeat the last command, replacing | 83 | | | 'str1' with 'str2'. | 84 | '--------------+------------------------------------------------------------' 85 | | !# | Refer to the entire command line typed so far. | 86 | '--------------'------------------------------------------------------------' 87 | 88 | Word Designators: 89 | (Word designators follow the event designators, separated by a collon ':') 90 | 91 | .--------------.------------------------------------------------------------. 92 | | | | 93 | | Designator | Description | 94 | | | | 95 | '--------------+------------------------------------------------------------' 96 | | 0 | The zeroth (first) word in a line (usually command name). | 97 | '--------------+------------------------------------------------------------' 98 | | n | The n-th word in a line. | 99 | '--------------+------------------------------------------------------------' 100 | | ^ | The first argument (the second word) in a line. | 101 | '--------------+------------------------------------------------------------' 102 | | $ | The last argument in a line. | 103 | '--------------+------------------------------------------------------------' 104 | | % | The word matched by the most recent string search. | 105 | '--------------+------------------------------------------------------------' 106 | | x-y | A range of words from x to y (-y is synonymous with 0-y). | 107 | '--------------+------------------------------------------------------------' 108 | | * | All words but the zeroth (synonymous with 1-$). | 109 | '--------------+------------------------------------------------------------' 110 | | x* | Synonymous with x-$ | 111 | '--------------+------------------------------------------------------------' 112 | | x- | The words from x to the second to last word. | 113 | '--------------'------------------------------------------------------------' 114 | 115 | Modifiers (modifiers follow word designators, separated by a colon): 116 | 117 | .--------------.------------------------------------------------------------. 118 | | | | 119 | | Modifier | Description | 120 | | | | 121 | '--------------+------------------------------------------------------------' 122 | | h | Remove a trailing pathname component, leaving the head. | 123 | '--------------+------------------------------------------------------------' 124 | | t | Remove all leading pathname component, leaving the tail. | 125 | '--------------+------------------------------------------------------------' 126 | | r | Remove a trailing suffix of the form .xxx, leaving the | 127 | | | basename. | 128 | '--------------+------------------------------------------------------------' 129 | | e | Remove all but the trailing suffix. | 130 | '--------------+------------------------------------------------------------' 131 | | p | Print the resulting command but do not execute it. | 132 | '--------------+------------------------------------------------------------' 133 | | q | Quotes the substituted words, escaping further | 134 | | | substitutions. | 135 | '--------------+------------------------------------------------------------' 136 | | x | Quotes the substituted words, breaking them into words at | 137 | | | blanks and newlines. | 138 | '--------------+------------------------------------------------------------' 139 | | s/old/new/ | Substitutes 'new' for 'old'. | 140 | '--------------+------------------------------------------------------------' 141 | | & | Repeats the previous substitution. | 142 | '--------------+------------------------------------------------------------' 143 | | g | Causes s/old/new/ or & to be applied over the entire | 144 | | | event line. | 145 | '--------------'------------------------------------------------------------' 146 | 147 | ============ History Behavior Modification via Shell Variables ============ 148 | 149 | .----------------.----------------------------------------------------------. 150 | | | | 151 | | Shell Variable | Description | 152 | | | | 153 | '----------------+----------------------------------------------------------' 154 | | HISTFILE | Controls where the history file gets saved. | 155 | | | Set to /dev/null not to save the history. | 156 | | | Default: ~/.bash_history | 157 | '----------------+----------------------------------------------------------' 158 | | HISTFILESIZE | Controls how many history commands to keep in HISTFILE | 159 | | | Default: 500 | 160 | '----------------+----------------------------------------------------------' 161 | | HISTSIZE | Controls how many history commands to keep in the | 162 | | | history list of current session. | 163 | | | Default: 500 | 164 | '----------------+----------------------------------------------------------' 165 | | HISTIGNORE | Controls which commands to ignore and not save to the | 166 | | | history list. The variable takes a list of | 167 | | | colon separated values. Pattern & matches the previous | 168 | | | history command. | 169 | '----------------'----------------------------------------------------------' 170 | 171 | ============ History Behavior Modification via `shopt' Command ============ 172 | 173 | .----------------.----------------------------------------------------------. 174 | | | | 175 | | shopt Option | Description | 176 | | | | 177 | '----------------+----------------------------------------------------------' 178 | | histappend | Setting the variable appends current session history to | 179 | | | HISTFILE. Unsetting overwrites the file each time. | 180 | '----------------+----------------------------------------------------------' 181 | | histreedit | If set, puts a failed history substitution back on the | 182 | | | command line for re-editing. | 183 | '----------------+----------------------------------------------------------' 184 | | histverify | If set, puts the command to be executed after a | 185 | | | substitution on command line as if you had typed it. | 186 | '----------------'----------------------------------------------------------' 187 | 188 | shopt options can be set by a `shopt -s option' and 189 | can be unset by a `shopt -u option'. 190 | 191 | =============================== Examples ================================== 192 | 193 | $ echo a b c d e (executes `echo ab c d e`) 194 | a b c d e 195 | 196 | $ echo !!:3-$ (executes `echo c d e`) 197 | c d e 198 | 199 | $ echo !-2:*:q (executes `echo 'a b c d e'`) 200 | a b c d e 201 | 202 | $ echo !-3:1:2:4:x (executes `echo 'a' 'b' 'd'`) 203 | a b d 204 | 205 | $ echo !-4:1-3:s/a/foo/:s/b/bar/:s/c/baz/ (executes `echo foo bar baz`) 206 | foo bar baz 207 | 208 | $ tar -xzf package-x.y.z.tgz 209 | ... 210 | $ cd !-1:$:r (executes `cd package-x.y.z`) 211 | package-x.y.z $ 212 | 213 | $ ls -a /tmp 214 | file1 file2 file3 ... 215 | 216 | $ ^-a^-l^ (executes `ls -l /tmp`) 217 | -rw------- 1 user user file1 218 | ... 219 | 220 | =========================================================================== 221 | 222 | .---------------------------------------------------------------------------. 223 | | Peteris Krumins (peter@catonmat.net), 2008.02.15 | 224 | | http://www.catonmat.net - good coders code, great reuse | 225 | | | 226 | | Released under the GNU Free Document License | 227 | '---------------------------------------------------------------------------' 228 | 229 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | This is the bash history cheat sheet. It summarizes everything there is to 2 | know about working efficiently with command line history in bash. 3 | 4 | It was created by Peteris Krumins (peter@catonmat.net). 5 | His blog is at http://www.catonmat.net -- good coders code, great reuse. 6 | 7 | The cheat sheet is released under GNU Free Document License. 8 | 9 | The cheat sheet was created as supplementary material to my popular article 10 | "The Definitive Guide to Bash Command Line History". This article can be read 11 | on my website at the following address: 12 | 13 | http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/ 14 | 15 | This cheat sheet is the 3rd in the series of working efficiently on the 16 | command line. 17 | 18 | The first was Bash Emacs Editing Mode Cheat Sheet: 19 | http://www.catonmat.net/blog/bash-emacs-editing-mode-cheat-sheet/ 20 | 21 | And the second was Bash Vi Editing Mode Cheat Sheet: 22 | http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/ 23 | 24 | ------------------------------------------------------------------------------ 25 | 26 | The bash history cheat sheet contains the following topics: 27 | 28 | * Bash in emacs mode history keyboard shortcuts. 29 | * Bash in vi mode history keyboard shortcuts. 30 | * History behavior modification via shell variables. 31 | * History behavior modification via shopt command. 32 | * History expansion. 33 | * Event designators. 34 | * Word designators. 35 | * Word designator modifiers. 36 | * History expansion examples. 37 | 38 | It's available in .txt (ascii), .pdf and latex (.tex) formats. The latest 39 | version of this cheat sheet can always be downloaded here: 40 | 41 | .txt: http://www.catonmat.net/download/bash-history-cheat-sheet.txt 42 | .pdf: http://www.catonmat.net/download/bash-history-cheat-sheet.pdf 43 | .tex: http://www.catonmat.net/download/bash-history-cheat-sheet.tex 44 | 45 | To create a new .pdf from latex's .tex, use pdflatex command: 46 | 47 | $ pdflatex bash-history-cheat-sheet.tex 48 | 49 | This will produce bash-history-cheat-sheet.pdf. 50 | 51 | ------------------------------------------------------------------------------ 52 | 53 | 54 | Have fun! This cheat sheet should make you much more productive on the 55 | command line. :) 56 | 57 | 58 | Sincerely, 59 | Peteris Krumins 60 | http://www.catonmat.net 61 | 62 | --------------------------------------------------------------------------------