Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active May 4, 2024 16:58
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
@albcorp
Copy link

albcorp commented Jul 6, 2021

@Luciesarah Take a look at one of the primers on the unix shell like https://www.ks.uiuc.edu/Training/Tutorials/Reference/unixprimer.html

In that one, it is covered at 2

@Luciesarah
Copy link

@albcorp Thanks a lot. I'm happy that it works now. Also thanks for the link, it helped me to understand the issue better. Have e great day :]

@cdbennett
Copy link

ctrl + v in standard bash prints the verbose version of the next key that is pressed:

Actually to be precise, Ctrl+v causes the next key typed to be inserted literally into the command, instead of interpreting special keys as line editing/shell control characters. E.g. The Ctrl+v Tab will insert the literal ASCII character 9 into the command line, instead of bash trying to execute argument completion.

This is a vi shortcut that bash inherited.

Oddly enough, the choice of Ctrl+y for paste comes from Emacs. (In Emacs, Ctrl+k = kill (cut), Ctrl+y = yank (paste))

@szuro
Copy link

szuro commented Oct 15, 2021

ctrl +x ctrl +e opens a text editor in which you can edit the current command. emacs by default, it can be changed by defining EDITOR variable, for example $ export EDITOR=vim.

@LawranceFung
Copy link

How exactly do you press "Where x is any character, moves the cursor forward to the next occurance of x", and "Where x is any character, moves the cursor backwards to the previous occurance of x"?

@SonkeWohler
Copy link

@LawranceFung You press ctrl+] followed by x. Same with ctrl+alt+] followed by x.

@LawranceFung
Copy link

@SonkeWohler not working for me in git bash

@SonkeWohler
Copy link

SonkeWohler commented Feb 1, 2022

@LawranceFung yeah, git bash is quite limited. It's main purpose is to providee the git cli, not to provide proper bash functionality.

Try out WSL2 if you want to do anything serious but don't want to switch to Linux (yet ;)

@2dluis
Copy link

2dluis commented Feb 1, 2022

That header image is my gonna be my new tattoo lol

@LawranceFung
Copy link

@SonkeWohler I have ubuntu on another partition, but develop mostly for windows. I also haven't found a suitable replacement on Linux for AutoHotkey.

@Wrongtown
Copy link

@LawranceFung if you ever do I'll be interested to know about it; AutoHotKey is a handy little thing.

@SonkeWohler
Copy link

SonkeWohler commented Feb 2, 2022

@LawranceFung Have you seen this? It seems to be like AutoHotkey for X11 (which is a bit of a shame I only find it now since X11 is on its way out), but I haven't used it since I switched to mostly working on CLI.

That said, I think there is less demand for Linux Desktop scripting for several reasons:

  • for anything not inherently GUI you have bash and many projects have a decent cli for it.
    • For example, I personally mostly wanted to remap my keys, which is covered by something like xmodmap and its scripting already.
    • Oftentimes the CLI can be more powerful than the GUI if the latter is mostly a wrapper around the former, but that depends on the kinds of projects you use. Git would be a prime example though.
  • the DEs on Linux are inherently more configurable and some even have their own cli to allow scripting certain things (like on my system I can play with kstart5 and the like to manipulate windows etc).
    • This is not usually their focus so not as fleshed out at AutoHotkey, but it exists so you can meet some people's needs that way.
  • Linux has a whole set of DEs plus most of them now have an X11 and a Wayland versions plus variations/derivatives. You can't make a universial Linux desktop scripting engine without avoiding much (or at least some) of the fine-grained control that would have to be implemented very differently for each DE. Or at least I don't think it can be very easy to do. The Linux project has achieved crazier things though.

@LawranceFung
Copy link

There are still many things that force me to use the GUI, namely webapps and electron apps. Although clumsy, AHK can map macros to clicks and mouse movement.
I haven't looked at AutoKey but as you said, it lacks the time-testedness and broad support of AHK.

@eugene-kuntsevich
Copy link

How to copy any word which I typed to cmd?
Let's say I want to use command like:
gradle :my-project-with-long-name:clean :my-project-with-long-name:build
So how could I copy-paste :my-project-with-long-name: in this case for excluding typing the same long word several times?

@rodezf
Copy link

rodezf commented Jun 14, 2022

Can not fork it . I'm new In the world of coding Can you tell me how can I keep this piece of good work in safe in amongst my other repositories?

@ashleyghooper
Copy link

ashleyghooper commented Aug 6, 2022

How to copy any word which I typed to cmd? Let's say I want to use command like: gradle :my-project-with-long-name:clean :my-project-with-long-name:build So how could I copy-paste :my-project-with-long-name: in this case for excluding typing the same long word several times?

If you're on a Unix-like system, one way would be to use Ctrl + x, Ctrl + e to open the current command line in your preferred visual editor (as defined by the $VISUAL shell variable, if set; failing that the $EDITOR variable is tried next, and finally a system-wide default editor will be used), Then edit the line in the editor, e.g. for vi or vim, using v for visual mode to select the text to copy, then expand the selection using the navigation keys, then y to yank (copy) it, then move to where you want it and paste with p. Your call whether the extra effort to do it this way is worth it though. :)

@d2weber
Copy link

d2weber commented Jan 12, 2023

Actually alt + * expands the autocompletion. Can be very useful.

@excited-bore
Copy link

Ctrl + s isn't reverse search, it's basically for 'pausing the tty'. Ctrl+q is for resuming said pause.
Also Ctrl-left/Ctrl-Right do what Alt+b/Alt+f do respectively.
Also, Shift+Pageup/Shift+PageDown to scroll through buffer is so good.

@MichaelMai2000
Copy link

MichaelMai2000 commented Feb 9, 2023

@LawranceFung You press ctrl+] followed by x. Same with ctrl+alt+] followed by x.

@SonkeWohler I have tried on my WSL2 with Ubuntu 22.04 and also ssh to a real headless Ubuntu 22.04. This didn't work on both of them. What am I missing here? Thanks.

edit: I have managed to get the first key bind, first press ctrl and hold, then press ], release both, and finally press x. But when I copy the pattern of this, "ctrl+alt+] followed by x" will just insert x without moving the cursor at all.

@SonkeWohler
Copy link

@MichaelMai2000

edit: I have managed to get the first key bind, first press ctrl and hold, then press ], release both, and finally press x. But when I copy the pattern of this, "ctrl+alt+] followed by x" will just insert x without moving the cursor at all.

It works fine on my system. Like you said, press and hold ctrl, alt, and ] at the same time, then let them all go, then press x.

I used to try to use [ instead of ] when searching backwards, probably because that is how vim tends to set things up and I just didn't notice I was doing it wrong.

I know WSL sometimes has problems, but the fact that you got the forward working tells me stuff is set up correctly for you (~/.inputrc is usually where this stuff is defined), so probably human error just like me.

@chinyu0704
Copy link

thanks

@doroshenkoSaac
Copy link

Thanks bro!

@sudofox
Copy link

sudofox commented Apr 7, 2023

would you be able to add a white outline on the top image so i can see it on github's dark mode?

@gitressa
Copy link

gitressa commented Jun 2, 2023

Great list, thanks! I have been wanting to delete the word FROM the cursor and learning about alt + d is very helpful.

Moving back or forward one word either with ctrl or alt key and arrow keys is possible, I believe. Should this be added?

command description
alt + f move cursor FORWARD one word (same as alt + right arrow or ctrl + right arrow)
alt + b move cursor BACK one word (same as alt + left arrow or ctrl + left arrow)

Also, it may be helpful to know that shift + ctrl + v pastes from clipboard, and ctrl + y pastes from the cut-buffer, at least in Ubuntu. An example:

  • Create this string: 111 222
  • Highlight 111 and copy it with shift + ctrl + c
  • Place the cursor before 222 and press alt + d to cut it
  • Press shift + ctrl + v and ctrl + y repeatedly, and get 111222111222111222111222

@SonkeWohler
Copy link

@gitressa

Also, it may be helpful to know that ctrl + v pastes from clipboard, and ctrl + y pastes from the cut-buffer. An example:

  • Create this string: 111 222
  • Highlight 111 and copy it with ctrl + c (or ctrl + shift + c)
  • Place the cursor before 222 and press alt + d to cut it
  • Press ctrl + v and ctrl + y repeatedly, and get 111222111222111222111222

That behaviour of ctrl + v is not part of bash but of your terminal emulator (Kitty, Alacritty, etc), which captures it and sends your system clipboard contents instead. In pure bash ctrl + v will insert the next "character" you type literally, e.g. ctrl + v followed by backspace inserts ^?, ctrl + v followed by enter inserts ^M.

Similarly for ctrl + c it would send an interrupt, but your emulator captures it first and copies to system clipboard instead.

ctrl + y is bash internal, hence why it comes up in this list. As such, though, it doesn't have access to your system clipboard, it is entirely separate. Hence the behaviour you describe with both clipboards having different contents.

When your emulator is not setup to overwrite ctrl + v you can usually still insert with shift + insert. Many emulators also use ctrl + shift + v or more rarely some combination with space.

@gitressa
Copy link

gitressa commented Jun 3, 2023

Thanks for clarifying. I use Ubuntu, and forgot that I have changed the classic "paste from clipboard" from shift + ctrl +v to ctrl + v, so I have updated my post, to use the standard shortcut (shift + ctrl +v) and also added that this is in Ubuntu. Sorry things got mixed up in my first version.

@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?

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