LinuxCLI/Bash shell in Cheatsheet
Appearance
New cheatsheet: bash readline keys, history expansion, completion, terminal keys. AI-assisted (RonzzWikiCowriter) (via create-page on MediaWiki MCP Server) |
No edit summary |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
{{DesktopLinux}} | {{DesktopLinux}} | ||
<blockquote> | |||
Notation: | |||
* <code>C-a</code> Ctrl+A | |||
* <code>M-f</code> is Alt+F (or ESC then <code>f</code> when no Alt key is available) | |||
</blockquote> | </blockquote> | ||
| Line 21: | Line 22: | ||
|} | |} | ||
== Delete, and paste back == | |||
Deleted text is not immediately lost: it goes to the kill ring and can be pasted back later: | |||
{| class="wikitable" | {| class="wikitable" | ||
| Line 50: | Line 45: | ||
| <code>C-_</code> or <code>C-x C-u</code> || undo | | <code>C-_</code> or <code>C-x C-u</code> || undo | ||
|} | |} | ||
== History == | == History == | ||
| Line 93: | Line 51: | ||
! Key !! Action | ! Key !! Action | ||
|- | |- | ||
| <code> | | <code>upArrow</code> / <code>downArrow</code> || previous / next command | ||
|- | |- | ||
| <code>C-r</code> || reverse incremental search: type a substring, <code>C-r</code> again for the next older match | | <code>C-r</code> || reverse incremental search: type a substring, <code>C-r</code> again for the next older match | ||
|- | |- | ||
| <code>C- | | <code>C-g</code> || aborts the search and gives back the line you were typing | ||
|- | |- | ||
| <code>M-p</code> / <code>M-n</code> || non-incremental search backward / forward for a string you type after the prompt | | <code>M-p</code> / <code>M-n</code> || non-incremental search backward / forward for a string you type after the prompt | ||
| Line 104: | Line 62: | ||
|- | |- | ||
| <code>M-C-y</code> || insert the first argument of the previous command; with a number, the nth (0 is the command itself) | | <code>M-C-y</code> || insert the first argument of the previous command; with a number, the nth (0 is the command itself) | ||
|- | |- | ||
| <code>C-x C-e</code> || edit the line in <code>$VISUAL</code> or <code>$EDITOR</code>, then run the result | | <code>C-x C-e</code> || edit the line in <code>$VISUAL</code> or <code>$EDITOR</code>, then run the result | ||
| Line 111: | Line 67: | ||
<blockquote> | <blockquote> | ||
<code>C-r</code> | While <code>C-r</code>is active, any movement key (<code>C-a</code>, <code>M-b</code>, ...) stops the search and accepts the current match for editing as the new command line. | ||
</blockquote> | </blockquote> | ||
== History expansion == | == History expansion == | ||
| Line 145: | Line 91: | ||
mkdir -p /srv/app/releases/2026-11-19 | mkdir -p /srv/app/releases/2026-11-19 | ||
cd !$ # → cd /srv/app/releases/2026-11-19 | cd !$ # → cd /srv/app/releases/2026-11-19 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<blockquote> | <blockquote> | ||
Expansion | Expansion works anywhere on the command line including inside double but not single quotes: | ||
* <code>echo "!$"</code> expands | |||
* <code>echo '!$'</code> does not. | |||
</blockquote> | </blockquote> | ||
| Line 161: | Line 108: | ||
|- | |- | ||
| <code>M-?</code> or <code>M-=</code> || list the possible completions | | <code>M-?</code> or <code>M-=</code> || list the possible completions | ||
|- | |- | ||
| <code>M-/</code> / <code>M-$</code> / <code>M-~</code> || complete a filename / a variable / a username after <code>~</code> | | <code>M-/</code> / <code>M-$</code> / <code>M-~</code> || complete a filename / a variable / a username after <code>~</code> | ||
| Line 172: | Line 117: | ||
| <code>C-v</code> or <code>C-q</code> || insert the next key literally (a Tab, a <code>C-m</code>, ...) | | <code>C-v</code> or <code>C-q</code> || insert the next key literally (a Tab, a <code>C-m</code>, ...) | ||
|} | |} | ||
== Screen and processes == | == Screen and processes == | ||
| Line 207: | Line 144: | ||
== Change the bindings == | == Change the bindings == | ||
=== <code>~/.bashrc</code> === | |||
apply to Bash only: | |||
<syntaxhighlight lang="bash" copy> | <syntaxhighlight lang="bash" copy> | ||
| Line 219: | Line 160: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== <code>~/.inputrc</code> === | |||
apply to every program using Readline (Bash, <code>python</code>, <code>psql</code>, <code>gdb</code>, ...): | |||
<syntaxhighlight lang="text" copy> | <syntaxhighlight lang="text" copy> | ||
| Line 225: | Line 168: | ||
set show-all-if-ambiguous on # Tab lists the candidates at once instead of beeping | set show-all-if-ambiguous on # Tab lists the candidates at once instead of beeping | ||
set completion-ignore-case on | set completion-ignore-case on | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== For more == | == For more == | ||
Latest revision as of 13:09, 19 November 2026
Quick reference for smart people — part of our Linux cheatsheets collection.
Notation:
C-aCtrl+AM-fis Alt+F (or ESC thenfwhen no Alt key is available)
Move around the line
| Key | Action |
|---|---|
C-a / C-e |
beginning / end of line |
C-b / C-f |
one character back / forward |
M-b / M-f |
one word back / forward |
M-< / M-> |
oldest / newest entry in the history |
Delete, and paste back
Deleted text is not immediately lost: it goes to the kill ring and can be pasted back later:
| Key | Action |
|---|---|
C-w |
delete the word before the cursor, up to whitespace (unix-word-rubout)
|
M-DEL |
delete the word before the cursor, word characters only (backward-kill-word)
|
M-d |
delete the word after the cursor (kill-word)
|
C-u |
delete from the line start to the cursor |
C-k |
delete from the cursor to the end of the line |
C-y |
paste back the last deleted text (yank) |
M-y |
replace the pasted text with older deleted text, only right after C-y
|
C-_ or C-x C-u |
undo |
History
| Key | Action |
|---|---|
upArrow / downArrow |
previous / next command |
C-r |
reverse incremental search: type a substring, C-r again for the next older match
|
C-g |
aborts the search and gives back the line you were typing |
M-p / M-n |
non-incremental search backward / forward for a string you type after the prompt |
M-. |
insert the last argument of the previous command; press again for the command before it |
M-C-y |
insert the first argument of the previous command; with a number, the nth (0 is the command itself) |
C-x C-e |
edit the line in $VISUAL or $EDITOR, then run the result
|
While
C-ris active, any movement key (C-a,M-b, ...) stops the search and accepts the current match for editing as the new command line.
History expansion
!! # rerun the previous command
!$ # last argument of the previous command
!^ # first argument
!* # all arguments
!:2 # 2nd argument
!-2 # the command before the previous one
!ssh # most recent command starting with ssh
!?docker? # most recent command containing docker
^old^new^ # rerun the previous command, first old replaced by new
!!:gs/old/new/ # rerun it, every old replaced by new
!$:h # last argument, minus its last path component: /a/b/c.pdf → /a/b
!$:t # last argument, basename only: /a/b/c.pdf → c.pdf
sudo !! # the command you forgot to prefix with sudo
mkdir -p /srv/app/releases/2026-11-19
cd !$ # → cd /srv/app/releases/2026-11-19
Expansion works anywhere on the command line including inside double but not single quotes:
echo "!$"expandsecho '!$'does not.
Completion and typing helpers
| Key | Action |
|---|---|
Tab |
complete the word; press twice to list the candidates |
M-? or M-= |
list the possible completions |
M-/ / M-$ / M-~ |
complete a filename / a variable / a username after ~
|
M-C-e |
expand the line as the shell would (aliases, variables, ~), then leave it for review
|
M-# |
comment the line out with # and keep it in the history
|
C-v or C-q |
insert the next key literally (a Tab, a C-m, ...)
|
Screen and processes
| Key | Action |
|---|---|
C-l |
clear the screen, keep the line being typed |
M-C-l |
clear the screen and the scrollback buffer |
C-c |
interrupt the running command |
C-d |
end of input: logs out an interactive shell, or deletes the character under the cursor |
C-z |
suspend the running command; fg brings it back, bg continues it in the background
|
C-s / C-q |
freeze / resume terminal output: an accidental C-s looks like a hang, press C-q
|
sleep 100 # C-z: suspended, back at the prompt
bg # continue it in the background
fg # or bring it to the foreground
jobs # what is running or suspended in this shell
Change the bindings
~/.bashrc
apply to Bash only:
set -o vi # vi keys (ESC, k, j, dd, ...) instead of emacs keys
set -o emacs # back to the default
bind -p # every key and the function bound to it
bind -p | grep -i yank # what is bound to yanking
bind -l # names of all bindable functions
bind '"\C-o": "| less\n"' # macro: C-o types ' | less' and presses Enter
bind -x '"\C-x\C-l": ls -l' # shell command whose output is shown, the line stays
bind '"\C-h": backward-delete-char' # rebind one key
~/.inputrc
apply to every program using Readline (Bash, python, psql, gdb, ...):
set editing-mode vi
set show-all-if-ambiguous on # Tab lists the candidates at once instead of beeping
set completion-ignore-case on
For more
- Readline interaction — the short official tour: moving, killing, searching
- History interaction — every
!form, word designator and modifier - GNU Readline manual — every bindable command and variable
- Readline init file —
~/.inputrcsyntax man readline,man bash(search/^READLINE),bind -p