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 Combination | Description | Notes |
CTRL-A | Move to beginning of line | |
CTRL-B | Move backward on character | |
CTRL-C | Kill Command | See CTRL-Z |
CTRL-D | Logout/backward one character | see exit |
CTRL-E | Move to the end of the line | |
CTRL-F | Move forward one character | |
CTRL-G | Abort editing current command | |
CTRL-H | Delete one character | |
CTRL-K | Delete to the end of the line | See CTRL-U |
CTRL-L | Clear screen. | See clear |
CTRL-N | Next line in command history | |
CTRL-O | RETURN, and display next line | |
CTRL-P | Previous line in command history | |
CTRL-Q | Resume shell output that was suspended | See CTRL-S |
CTRL-R | Search backwards | |
CTRL-S | Suspend shell output | See CTRL-Q |
CTRL-T | Transpose two characters | |
CTRL-U | Kill backwards to beginning of the line | See CTRL-K |
CTRL-W | Kill word behind cursor | |
CTRL-Y | Undo last item killed | See CTRL-W, CTRL-U |
CTRL-Z | Stop running command | See fg, bg, kill %<Number> |
ALT-B | Move backward one word | See ALT-F |
ALT-D | Delete next word | |
ALT-F | Move forward one word | See ALT-B |
ALT-C | Capitalize letter under the cursor to end of word | See ALT-U, ALT-L |
ALT-L | Uncapitalize letter under the cursor to end of word | See ALT-C, ALT-U |
ALT-U | Capitalize every character under cursor to end of word | See ALT-C, ALT-L |
Other notable shortcuts
Keys | Description | Notes |
CTRL-X, CTRL-E | Executes editor on current line | |
history | Display the command line history | |
!! | Repeat the last line/command | Be 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 string | eg !ansible-play executes the last command in history that refers to ansible-playbook my.yml |
Useful Bash Variables
Variable | Description | Notes |
HISTSIZE | The number of records to keep in history | Default 1000, set to 0 to disable. Set to undef, or -1 for unlimited history. This is in memory until you logout. |
HISTFILESIZE | The number of records to write back to disk on logout | Default 1000, set to 0 to disable. Set to undef, or -1 for unlimited history. |
HISTCONTROL | Colon separated list of values | ignoredups: 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 -w | Command: save your history to disk now | |
TMOUT | Sets a session timeout for the shell | Useful to logout idle sessions and write history to disk |
HISTTIMEFORMAT | Add a timestamp to history entries | Example: export HISTTIMEFORMAT=”%h %d %H:%M%S” Example: export HISTTIMEFORMAT=”%F %T” |
shopt -s histappend | Append commands to history file instead of overwriting. | Use shopt -u histappend to revert |
PROMPT_COMMAND=’history -a’ | Write the history immediately after executing a command | By default, bash writes the history file at logoff. This writes it immediately |
HISTIGNORE | Colon separated list of values | export HISTIGNORE=”rm:history:ps” will not write commands rm, history, ps to the history file. |