Share

Updated: September 16th, 2012

imageDuring my day-to-day activities, I use the Bash shell a lot. My #1 policy is to optimize the most frequently used activities as much as possible, so I’ve compiled these handy bash shortcuts and hints (tested in SecureCRT on Windows and Konsole on Linux). The article only touches on the default bash mode – emacs, not vi. If you haven’t specifically assigned your shell mode to vi (set –o vi), you’re almost certainly using the emacs mode. Learn these and your shell productivity will skyrocket, I guarantee it.

Update #1: In response to a few people saying this list is too short and “[he] could've added something to it, to atleast make it look longer” (quote from one of Stumbleupon reviewers), I want to clarify something. I deliberately did not include every single bash shortcut there is. I included what I personally thought were the best and most useful commands. I did not want to make the list too cluttered and wanted the cheat sheet to fit on one page without going to a smaller font size.

image 

Download Version 1.3 (03/22/12):

And, for quick reference, here's the ugly html version:

Keyboard shortcut

Action

Navigation

Ctrl-A

Go to the beginning of the line (note that if you use GNU screen, you can use the Home button to do this, especially considering that Ctrl-A is a special control character in screen).

Ctrl-E

Go to the end of the line (note that if you use GNU screen, you can use the End button to do this).

Alt-B (or ESC, left arrow)

Jump back one word using a non-alphanumeric character as delimiter.

Alt-F (or ESC, right arrow)

Jump forward one word using a non-alphanumeric character as delimiter.

Ctrl-PGUP or Shift-PGUP

This may or may not work, and it works differently on different console apps. It will either scroll up one line at a time, 1 page at a time, or it may not work at all. I'm inclined to think it's not a bash shortcut at all.

Ctrl-PGDN or Shift-PGDN

Same as the above but scrolling is done in the opposite direction.

Up/Down

Previous/Next command in history. This one is way too obvious but I'm including it for completeness.

Ctrl-R

History search. For example, Ctrl-R svn Ctrl-R Ctrl-R … will cycle through all recently run commands with the ‘svn’ in them. It is one of the most useful shortcuts in bash.

Ctrl-O

Takes whatever line was after the line you selected with Ctrl-R and makes it your next command.

Command Line Manipulation

Ctrl-W

Cut one word backwards using white space as delimiter.

Alt-BACKSPACE

Cut one word backwards using a non-alphanumeric character as delimiter (different from Ctrl-W, for example, abc;bcd will cut to abc;).

Ctrl-K

Cut everything forward to end of line.

Ctrl-U

Cut everything backwards to beginning of line.

Ctrl-T

Transpose the current character with the previous one. I almost never use this. Never mind, I never use it, but someone might find it useful.

Alt-T

Transpose the word at cursor with the one before cursor. In other words, swap them around.

Ctrl-Y

Paste whatever was cut by the last cut command.

Ctrl-V

Insert the next character literally. For example, Ctrl-V TAB inserts the actual TAB character. This shortcut is often misunderstood because of mistyping Ctrl-V and not realizing what it does.

Ctrl-_

Undo the last command. Don’t forget – it’s Ctrl-Shift-MINUS, not Ctrl-MINUS.

Alt-R

Revert all changes to current line. Very useful if you accidentally modify a command in history.

Alt-U/Alt-L/Alt-C

Uppercase/lowercase/capitalize from cursor to end of word and move cursor past end of word.

Terminal control

Ctrl-L

Clear screen while keeping whatever is already typed in the command line intact.

Ctrl-S

Suspend currently running terminal.

Ctrl-Q

Unsuspend the terminal suspended by Ctrl-S. You need to be aware of this shortcut because 99% of the time you’ve accidentally pressed Ctrl-S and need to undo its effects.

Ctrl-Z

Suspend the currently running process (usually followed by bg to resume it in the background or fg to resume in the foreground).

TAB

Autocomplete. Start typing, then hit TAB. You will either get a list of possible completion values (2 TABs needed) or the only choice will be filled in (only 1 TAB is needed). This shortcut is quite obvious and well known, so I put it at the bottom of the list.

Tip: By the way, as duly noted in the comments, all of these tricks work on the mysql command line, so you can, for example, ctrl-R through your previously executed mysql commands.

Hope you guys will find this list helpful. I think it is relatively complete, but feel free to add any omissions.

● ● ●

Artem Russakovskii is a San Francisco programmer, blogger, and future millionaire (that last part is in the works). Follow Artem on Twitter (@ArtemR) or subscribe to the RSS feed.

In the meantime, if you found this article useful, feel free to buy me a cup of coffee below.



Share
  • http://omninoggin.com Thaya Kareeson

    I use these sometimes, but I find that "set -o vi" as you said in the beginning solves all of my productivity woes.

  • Erkan

    Why is this blogentry on planetMYSQL.org?

  • http://beerpla.net Artem Russakovskii

    Version 1.0 of the cheat sheet is up in PDF, DOC, and DOCX formats. Subscribe to comments using the link below for updates.

  • Cliff

    @Erkan: probably because not everyone uses phpMyAdmin. sometimes it's easier for me to SSH into my server, and at that point, I'm at the mercy of bash (or it's at my mercy…). So, not directly related to MySQL, but very practically useful for many admins. Cheers!

  • http://beerpla.net Artem Russakovskii

    @Cliff, thank you. Indeed, you can use all of these tricks, including ctrl-R on the mysql prompt, so they are very handy for mysql admins and users alike.

  • Ikai Lan

    Suspend and unsuspend totally pwned me when I was a n00b first moving from Windows to Linux and I kept hitting Control-S to save stuff in VIM.

    Note that not all of these work for OS X, specifically, the Alt commands. And dude, how come you put Control-T in here but not Control-Z?

  • http://beerpla.net Artem Russakovskii

    Thanks, Ikai, don't know why ctrl-Z evaded the list, it's quite important indeed.

    Updated to v1.11:
    - added ctrl-Z
    - cleaned up some formatting issues

  • mannan

    could anyone tell me a site for bash……very gud one…..which starts from the beginning n goes till the end….

  • zhi

    thanks for cheat sheet.

  • http://www.catonmat.net Peteris Krumins

    Hi Artem,

    Have you seen my two articles on bash vi and emacs editing mode shortcuts?

    Mine also have cheat sheets with them (one for each mode).

    They are here:

    http://www.catonmat.net/blog/bash-vi-editing-mode-cheat-sheet/

    http://www.catonmat.net/blog/bash-emacs-editing-mode-cheat-sheet/

    Peteris

  • http://beerpla.net Artem Russakovskii

    Peteris, thanks for the links – I haven't seen them before. ;) Your cheat sheets are quite complete (2 pages, small font).

    "M-Rubout" – you weird Latvians :D

  • http://www.catonmat.net Peteris Krumins

    Oh, I see. I should probably make a "come-back" article on catonmat for the new readers who haven't seen them.

    There is also "The Definitive Guide to Command Line History":

    http://www.catonmat.net/blog/the-definitive-guide-to-bash-command-line-history/

    Rubout is the official name of the key, afaik! ;)

  • http://beerpla.net Artem Russakovskii

    Version 1.2:
    - added Alt-U/L/C: Uppercase/lowercase/capitalize from cursor to end of word and move cursor past end of word.
    - added Alt-R: Revert all changes to current line. Very useful if you accidentally modify a command in history.
    - added Ctrl-_: Undo the last command. Don’t forget – it’s Ctrl-Shift-MINUS, not Ctrl-MINUS.
    - various changes to formatting and wording.

  • Ernie

    This will come in handy for a lot of linux users I am going to try to learn more about this myself I have used linux for 8 years but have not yet played with scripting much. I found a site from searching the Linux Search Engine that has some basic info in it that may come in handy for some who are getting acquainted with this. Hope this is helpful to someone. bash shell info

  • http://planetozh.com/ Ozh

    Cool list, but to be a real cheat sheet it needs to be a printable A4 pdf :)

  • http://beerpla.net Artem Russakovskii

    @Ozh
    do the included PDF and Word formats not work for you? :-/

  • http://foohack.com Isaac Z. Schlueter

    These aren't "bash" shortcuts, per se. They're readline shortcuts. Any program that uses readline to manage input will support them. That's why they work in MySQL, as well as many other programs. In fact, if you have the rlwrap program installed, you can wrap ANY program in a readline instance! Try installing it, and then typing:

    $ rlwrap cat

    The readline utility can also be highly customized by using the ~/.inputrc file. I recommend checking it out:

    $ man readline

    For a list of handy bash shortcuts, check out http://commandlinefu.com

  • http://beerpla.net Artem Russakovskii

    @Isaac
    Thanks for the clarification on readline!

  • Unix Shells | Udshells

    These are soem very usefull tips, well laid out and easy to understand thanks for the post!

  • http://codercaste.com Spyros

    Really a great reference, i tend to use ctrl+a and ctrl+U quite a lot and TAB of course. They make life easier. Linux is really about shortcuts anyway.

  • teknoloji

    Thanks for sharing

  • http://www.pdftodocconverterpro.com Scott

    Thanks a lot of the tips.

  • http://www.associatedcontent.com/article/2368886/running_linux_on_a_virtual_machine.html Joe Poniatowski

    I like to use vi as the command-line editor, eg; 'set -o vi'. Once set, you can search through the command history with esc-K, and use vi command to edit the line (replace words, characters, etc.) It's generally easier than back-spacing and re-typing (for those familiar with vi).

  • http://omninoggin.com Thaya Kareeson

    What if you were on a Mac?? :)

    • http://beerpla.net Artem Russakovskii

      Then god help you, Thaya.

  • Bris

    Can i execute a shell script when i enter a particular pattern in my bash shell like an IP address?

  • Jeff S

    I download the files but they seem to be broken. The pdf doesn't work. The other two have woes as well. Is there an issue and can it be fixed??

    • http://beerpla.net Artem Russakovskii

      Hrm, you seem to be right. I'll take a look, wonder what happened to them.

    • http://beerpla.net Artem Russakovskii

      Alright, it should be fixed – there is apparently an incompatibility between WP-Minify and Download Counter (http://wordpress.org/extend/plugins/wp-downloadcounter). I'll get in touch with the author of WP-Minify.

  • whall

    This is a great list. Since you seem to have a line free, you might want to add CTRL-O right after CTRL-R. CTRL-O, once I learned it, became very useful when using RCS and other commands that go in pairs.

    Let's say you do your ci/co process:

    # rcs -i myfile.pl
    # ci -u myfile.pl
    # co -l myfile.pl

    Now the file is checked out and locked by you. You do lots of vim, perl -wc and all sorts of testing. Then you're ready to repeat the ci/co process.

    So you do your trusty CTRL-R, type
    "ci" and CTRL-R if needed to find the "ci -u myfile.pl" piece.

    Now, instead of pressing [Enter], you press CTRL-O. This presses [enter] for you, but automatically tees up the "co -l myfile.pl" line as your next line, waiting you to press [Enter].

    CTRL-O takes whatever line was after the line you selected with CTRL-R and makes it your next command.

    Hope this helps!

    • http://beerpla.net Artem Russakovskii

      That is ridiculously awesome. Thanks for the tip!

    • http://beerpla.net Artem Russakovskii

      Updated all the cheat sheets to v1.3 with this included.

  • didi

    > … it’s Ctrl-Shift-MINUS, not Ctrl-MINUS.

    I expected that, but in my bash shortcut Ctrl-MINUS also works and I don't know why, as bind lists only:
    "\C-x\C-u": undo
    "\C-_": undo
    Can you explain that?

    • didi

      OK, after further digging I see it is on keybord level as Ctrl-7 and Ctrl-/ also generate ^_ character.