Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active July 21, 2024 14:28
Show Gist options
  • Save tuxfight3r/60051ac67c5f0445efee to your computer and use it in GitHub Desktop.
Save tuxfight3r/60051ac67c5f0445efee to your computer and use it in GitHub Desktop.
Bash keyboard shortcuts

Bash Shortcuts

visual cheetsheet

Moving

command description
ctrl + a Goto BEGINNING of command line
ctrl + e Goto END of command line
ctrl + b move back one character
ctrl + f move forward one character
alt + f move cursor FORWARD one word
alt + b move cursor BACK one word
ctrl + xx Toggle between the start of line and current cursor position
ctrl + ] + x Where x is any character, moves the cursor forward to the next occurance of x
alt + ctrl + ] + x Where x is any character, moves the cursor backwards to the previous occurance of x

Edit / Other

command description
ctrl + d Delete the character under the cursor
ctrl + h Delete the previous character before cursor
ctrl + u Clear all / cut BEFORE cursor
ctrl + k Clear all / cut AFTER cursor
ctrl + w delete the word BEFORE the cursor
alt + d delete the word FROM the cursor
ctrl + y paste (if you used a previous command to delete)
ctrl + i command completion like Tab
ctrl + l Clear the screen (same as clear command)
ctrl + c kill whatever is running
ctrl + d Exit shell (same as exit command when cursor line is empty)
ctrl + z Place current process in background
ctrl + _ Undo
ctrl + x ctrl + u Undo the last changes. ctrl+ _ does the same
ctrl + t Swap the last two characters before the cursor
esc + t Swap last two words before the cursor
alt + t swap current word with previous
esc + .
esc + _
alt + [Backspace] delete PREVIOUS word
alt + < Move to the first line in the history
alt + > Move to the end of the input history, i.e., the line currently being entered
alt + ? display the file/folder names in the current path as help
alt + * print all the file/folder names in the current path as parameter
alt + . print the LAST ARGUMENT (ie "vim file1.txt file2.txt" will yield "file2.txt")
alt + c capitalize the first character to end of word starting at cursor (whole word if cursor is at the beginning of word)
alt + u make uppercase from cursor to end of word
alt + l make lowercase from cursor to end of word
alt + n
alt + p Non-incremental reverse search of history.
alt + r Undo all changes to the line
alt + ctl + e Expand command line.
~[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

History

command description
ctrl + r Search backward starting at the current line and moving 'up' through the history as necessary
crtl + s Search forward starting at the current line and moving 'down' through the history as necessary
ctrl + p Fetch the previous command from the history list, moving back in the list (same as up arrow)
ctrl + n Fetch the next command from the history list, moving forward in the list (same as down arrow)
ctrl + o Execute the command found via Ctrl+r or Ctrl+s
ctrl + g Escape from history searching mode
!! Run PREVIOUS command (ie sudo !!)
!vi Run PREVIOUS command that BEGINS with vi
!vi:p Print previously run command that BEGINS with vi
!n Execute nth command in history
!$ Last argument of last command
!^ First argument of last command
^abc^xyz Replace first occurance of abc with xyz in last command and execute it

Kill a job

n = job number, to list jobs, run jobs

kill %n

Example:

kill %1

References

  1. http://cnswww.cns.cwru.edu/php/chet/readline/readline.html
  2. https://github.com/fliptheweb/bash-shortcuts-cheat-sheet/blob/master/README.md
@DSc-de
Copy link

DSc-de commented Jun 23, 2023

Ctrl + s isn't reverse search, it's basically for 'pausing the tty'. Ctrl+q is for resuming said pause.

In that case most likely the underlying OS terminal device receives and interprets the shortcut. stty can be used to disable, e.g. stty -ixon. After disabling it on my GNOME terminal, expected history search is started on Ctrl + s . For more information see https://unix.stackexchange.com/questions/12107/how-to-unfreeze-after-accidentally-pressing-ctrl-s-in-a-terminal

@idimitroie
Copy link

idimitroie commented Jan 2, 2024

Hi,
I am very impressed, how did you make the figure?
Latex?

@busfahrer
Copy link

busfahrer commented Jan 4, 2024

Great list, I learned a lot.

One thing I would add:
In addition to !n (which runs the history entry n), you can also do !-n which runs the n-th previous command, e.g. !-2 would run the command that preceded your last command.

@jzk
Copy link

jzk commented Mar 17, 2024

Great list, I learned a lot.

One thing I would add: In addition to !n (which runs the history entry n), you can also do !-n which runs the n-th previous command, e.g. !-2 would run the command that preceded your last command.

good to learn that! but isn't pressing up twice easier?

@Eslam-Farg
Copy link

How to discard the current command (not executed yet) and move to a new line without printing anything or deleting the current line?
use case: if I need to copy that line and use it later after executing other commands.

@mamad-1999
Copy link

How to discard the current command (not executed yet) and move to a new line without printing anything or deleting the current line? use case: if I need to copy that line and use it later after executing other commands.

You can discard the current command without executing it and move to a new line by using the keyboard shortcut Ctrl+U. This clears the current line but saves it in a buffer, allowing you to paste it back later with Ctrl+Y.

@NTR0314
Copy link

NTR0314 commented Jul 4, 2024

What I find quite confusing is that CTRL + w and ALT + d are not exact opposites of each other.
CTRL + W will delete a space seperated word like word_num_1 word_num_2, while
ALT + d will only delete until the underscores. I wondering if it is configurable that ALT + d behaves the same as CTRL + W or if there is an inbuilt alternative?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment