Jump to content

LinuxCLI/Bash shell in Cheatsheet

From Wikibase

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