Jump to content

LinuxCLI/Bash shell in Cheatsheet

From Wikibase
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}}


The line you are typing at the prompt is edited by GNU Readline, so these keys work in Bash and in every other Readline program (<code>python</code>, <code>psql</code>, <code>gdb</code>, <code>mysql</code>, ...). They are pressed while the line is still on screen, before Enter.
<blockquote>
Notation:


<blockquote>
* <code>C-a</code> Ctrl+A
Notation: <code>C-a</code> is Ctrl+A. <code>M-f</code> is Alt+F, or ESC then <code>f</code> when no Alt key is available.
* <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:
|}
|}


<syntaxhighlight lang="bash" copy>
== Delete, and paste back ==
# the cursor starts at the end of the line
git commit -m 'fix typo in README'
# C-a      cursor to 'git'
# M-f M-f  cursor to 'fix'
# C-e      back to the end
</syntaxhighlight>


== 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
|}
|}
Deleted text is not lost: it goes to the kill ring and can be pasted back later, even on the next line.
<syntaxhighlight lang="bash" copy>
# cursor at the end of the line
vim /etc/nginx/sites-enabled/default
# C-w    →  vim                              kills back to the previous space
# M-DEL  →  vim /etc/nginx/sites-enabled/    kills one path component
# C-u kills from the cursor back to the start, C-k kills forward; both are undone by C-y
</syntaxhighlight>
== Transpose and change case ==
{| class="wikitable"
! Key !! Action
|-
| <code>C-t</code> || swap the 2 characters around the cursor
|-
| <code>M-t</code> || swap the 2 words around the cursor
|-
| <code>M-u</code> / <code>M-l</code> / <code>M-c</code> || upcase / downcase / capitalize the word
|}
<blockquote>
The case commands act on the word at the cursor and move forward, so pressing <code>M-u</code> at the end of the line changes nothing. Move back with <code>M-b</code> first.
</blockquote>
<syntaxhighlight lang="bash" copy>
echo hELLO world
# M-b M-b M-c  →  echo Hello world    capitalize (rest of the word is lowercased)
# M-b M-b M-l  →  echo hello world    downcase
# M-b M-b M-u  →  echo HELLO world    upcase
echo one two
# M-t  →  echo two one
</syntaxhighlight>


== History ==
== History ==
Line 93: Line 51:
! Key !! Action
! Key !! Action
|-
|-
| <code>C-p</code> / <code>C-n</code> || previous / next command
| <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-s</code> || the same, searching forward
| <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-o</code> || run this line, then fetch the next history entry for editing
|-
|-
| <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> leaves the matching command on the line: <code>C-g</code> aborts the search and gives back the line you were typing, any movement key (<code>C-a</code>, <code>M-b</code>, ...) stops the search and lets you edit the match, Enter runs it.
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>
<blockquote>
<code>C-s</code> is often swallowed by terminal flow control (see [[#Screen and processes]]); <code>stty -ixon</code> in <code>~/.bashrc</code> frees it.
</blockquote>
<syntaxhighlight lang="bash" copy>
# C-r  ssh        the most recent command containing 'ssh'; C-r again for older ones
# C-x C-e        a long loop: vim opens, edit it comfortably, save and quit, bash runs it
# C-o            step through a block of commands you ran earlier, editing each before running
</syntaxhighlight>


== 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
git status
^status^diff^                                    # → git diff
</syntaxhighlight>
</syntaxhighlight>


<blockquote>
<blockquote>
Expansion happens on the whole line, before it runs, and inside double quotes too: <code>echo "!$"</code> expands, <code>echo '!$'</code> does not. <code>set +H</code> turns expansion off; <code>shopt -s histverify</code> shows the expanded line for confirmation before running it.
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> || insert all 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>, ...)
|}
|}
<syntaxhighlight lang="bash" copy>
cd ~/Doc<Tab>              # → cd ~/Documents
ls /usr/shar<Tab>          # the single match is inserted, no list needed
# M-C-e on  ll ~/x/*
#  becomes  ls -l /home/user/x/*
# M-# on  rm -rf build/  →  #rm -rf build/  stored and recalled later, not run
</syntaxhighlight>


== 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>


Settings for every Readline program (Bash, <code>python</code>, <code>psql</code>, <code>gdb</code>, ...) live in <code>~/.inputrc</code>:
=== <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
"\e[1;5C": forward-word            # Ctrl+Right: one word forward
"\C-x\C-r": re-read-init-file      # C-x C-r: apply the edits without restarting the shell
</syntaxhighlight>
</syntaxhighlight>
<blockquote>
Bindings in <code>~/.bashrc</code> apply to Bash only; <code>~/.inputrc</code> applies to every program that uses Readline. <code>bind -p</code> prints the bindings in <code>~/.inputrc</code> syntax, so its output can be pasted there.
</blockquote>


== For more ==
== For more ==

Latest revision as of 13:09, 19 November 2026

Languages: English · français · Esperanto

Quick reference for smart people — part of our Linux cheatsheets collection.

Notation:

  • C-a Ctrl+A
  • M-f is Alt+F (or ESC then f when 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 "!$" expands
  • echo '!$' 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