Skip to content

Instantly share code, notes, and snippets.

@michaelcoyote
Last active July 30, 2020 16:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelcoyote/06f7ca2beab18ccca086d4b5b10f5f93 to your computer and use it in GitHub Desktop.
Save michaelcoyote/06f7ca2beab18ccca086d4b5b10f5f93 to your computer and use it in GitHub Desktop.

Bash key combinations and shortcuts

The bash shell uses a library called GNU Readline that provides easy and quick CLI key combination access to bash history, screen movement and line editing commands. I've collected some of these here along with some builtin bash shortcuts for history and other functions.

Note: The command bind -p will list all the keybindings and the readline functions they call.

Cursor Movement

command description
ctl + a Goto beginning of command line
ctl + e Goto end of command line
ctl + b Move back one character
ctl + f Move forward one character
alt + f Move cursor forward one word
alt + b Move cursor back one word
ctl + l Clear the screen (same as clear command)
ctl + ] <char> Move forward to next occurrence of <char>
alt + ctrl + [ <char> Move backwards to previous occurrence of <char>

Line editing

command description
alt + . Print the last argument (ie "vim file1.txt file2.txt" will yield "file2.txt")
esc + t Swap last two words before the cursor
ctl + d Delete the character under the cursor
ctl + h Delete character before the cursor
ctl + u Delete everything before cursor
ctl + k Delete everything after cursor
alt + backspace Delete previous word
ctl + w Delete the word before the cursor
ctl + t Swap the last two characters before the cursor
ctl + y Paste (if you used a previous command to delete)
ctl + z Place current process in background
ctl + _ Undo
ctl + x + ctl + e Open current line in $EDITOR

History

command description
!! Run previous command (e.g. sudo !!)
!cmd Run previous command that begins with cmd
!cmd:p Print last command in history beginning with cmd
!n Execute nth command in history
!$ Last argument of last command
!^ First argument of last command
alt + < Move to the first line in the history
alt + > Move to the end of the input history, i.e., the current line
ctl + r Search backward starting at the current line and moving 'up' through the history as necessary
crl + s Search forward starting at the current line and moving 'down' through the history as necessary
ctl + p Fetch the previous command from the history list, moving back in the list (same as up arrow)
ctl + n Fetch the next command from the history list, moving forward in the list (same as down arrow)

Job control

command description
ctl + c Kill current process
ctl + d Exit shell (same as exit command)

Other Misc.

command description
~[TAB][TAB] List all users
$[TAB][TAB] List all system variables
@[TAB][TAB] List all entries in your /etc/hosts file
[TAB] Auto complete
cd - Change to previous working directory
ctl + x + ctl + v Return the shell version

References

  1. Readline Documentation
  2. Bash Documentation
  3. bind command
  4. GNU Readline (Wikipedia)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment