Bash Shortcuts and Customization Cheatsheet

06-11-2021

The tables below list keyboard combinations and other customization to bash that I use on a regular basis. This is not an inclusive list. The man pages will have far more detail on what you can and can’t do.

Keyboard CombinationDescriptionNotes
CTRL-AMove to beginning of line
CTRL-BMove backward on character
CTRL-CKill CommandSee CTRL-Z
CTRL-DLogout/backward one charactersee exit
CTRL-EMove to the end of the line
CTRL-FMove forward one character
CTRL-GAbort editing current command
CTRL-HDelete one character
CTRL-KDelete to the end of the lineSee CTRL-U
CTRL-LClear screen.See clear
CTRL-NNext line in command history
CTRL-ORETURN, and display next line
CTRL-PPrevious line in command history
CTRL-QResume shell output that was suspendedSee CTRL-S
CTRL-RSearch backwards
CTRL-SSuspend shell outputSee CTRL-Q
CTRL-TTranspose two characters
CTRL-UKill backwards to beginning of the lineSee CTRL-K
CTRL-WKill word behind cursor
CTRL-YUndo last item killedSee CTRL-W, CTRL-U
CTRL-ZStop running commandSee fg, bg, kill %<Number>
ALT-BMove backward one wordSee ALT-F
ALT-DDelete next word
ALT-FMove forward one wordSee ALT-B
ALT-CCapitalize letter under the cursor to end of wordSee ALT-U, ALT-L
ALT-LUncapitalize letter under the cursor to end of wordSee ALT-C, ALT-U
ALT-UCapitalize every character under cursor to end of wordSee ALT-C, ALT-L
Common editing commands

Other notable shortcuts

KeysDescriptionNotes
CTRL-X, CTRL-EExecutes editor on current line
historyDisplay the command line history
!!Repeat the last line/commandBe careful with things like rm <relativepath>. You might delete something you didn’t expect too.
!<n>Execute line/command at history position <n>
!<string>Execute last command starting with stringeg !ansible-play executes the last command in history that refers to ansible-playbook my.yml
Command Execution Shortcuts

Useful Bash Variables

VariableDescriptionNotes
HISTSIZEThe number of records to keep in historyDefault 1000, set to 0 to disable. Set to undef, or -1 for unlimited history.
This is in memory until you logout.
HISTFILESIZEThe number of records to write back to disk on logoutDefault 1000, set to 0 to disable. Set to undef, or -1 for unlimited history.
HISTCONTROLColon separated list of valuesignoredups: ignore duplicate entries
ignorespace: ignore commands that start with a space. Good for sensitive commands (passwords/destructive commands)
ignoreboth: same as ignoredups:ignorespace
erasedups: all previous lines in the history list will be elimated
history -wCommand: save your history to disk now
TMOUTSets a session timeout for the shellUseful to logout idle sessions and write history to disk
HISTTIMEFORMATAdd a timestamp to history entriesExample: export HISTTIMEFORMAT=”%h %d %H:%M%S”
Example: export HISTTIMEFORMAT=”%F %T”
shopt -s histappendAppend commands to history file instead of overwriting.Use shopt -u histappend to revert
PROMPT_COMMAND=’history -a’Write the history immediately after executing a commandBy default, bash writes the history file at logoff. This writes it immediately
HISTIGNOREColon separated list of valuesexport HISTIGNORE=”rm:history:ps” will not write commands rm, history, ps to the history file.
Useful variables

Leave a Reply

Your email address will not be published. Required fields are marked *