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)
 
Clarify the C-y line. AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
Line 59: Line 59:
# M-DEL  →  vim /etc/nginx/sites-enabled/    kills one path component
# 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
# C-u kills from the cursor back to the start, C-k kills forward; C-y pastes either back
</syntaxhighlight>
</syntaxhighlight>



Revision as of 12:24, 19 November 2026

Languages: English · français · Esperanto

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

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 (python, psql, gdb, mysql, ...). They are pressed while the line is still on screen, before Enter.

Notation: C-a is 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
# 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

Delete, and paste back

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

Deleted text is not lost: it goes to the kill ring and can be pasted back later, even on the next line.

# 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; C-y pastes either back

Transpose and change case

Key Action
C-t swap the 2 characters around the cursor
M-t swap the 2 words around the cursor
M-u / M-l / M-c upcase / downcase / capitalize the word

The case commands act on the word at the cursor and move forward, so pressing M-u at the end of the line changes nothing. Move back with M-b first.

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

History

Key Action
C-p / C-n previous / next command
C-r reverse incremental search: type a substring, C-r again for the next older match
C-s the same, searching forward
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-o run this line, then fetch the next history entry for editing
C-x C-e edit the line in $VISUAL or $EDITOR, then run the result

C-r leaves the matching command on the line: C-g aborts the search and gives back the line you were typing, any movement key (C-a, M-b, ...) stops the search and lets you edit the match, Enter runs it.

C-s is often swallowed by terminal flow control (see #Screen and processes); stty -ixon in ~/.bashrc frees it.

# 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

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

Expansion happens on the whole line, before it runs, and inside double quotes too: echo "!$" expands, echo '!$' does not. set +H turns expansion off; shopt -s histverify shows the expanded line for confirmation before running it.

Completion and typing helpers

Key Action
Tab complete the word; press twice to list the candidates
M-? or M-= list the possible completions
M-* insert all 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, ...)
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

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

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

Settings for every Readline program (Bash, python, psql, gdb, ...) live in ~/.inputrc:

set editing-mode vi
set show-all-if-ambiguous on       # Tab lists the candidates at once instead of beeping
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

Bindings in ~/.bashrc apply to Bash only; ~/.inputrc applies to every program that uses Readline. bind -p prints the bindings in ~/.inputrc syntax, so its output can be pasted there.

For more