SEARCH

How-To Geek

Keyboard Shortcuts for Bash ( Command Shell for Ubuntu, Debian, Suse, Redhat, Linux, etc)

The default shell on most Linux operating systems is called Bash. There are a couple of important hotkeys that you should get familiar with if you plan to spend a lot of time at the command line. These shortcuts will save you a ton of time if you learn them. 

 



Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L               Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names

Note that some of these commands may not work if you are accessing bash through a telnet/ssh session, or depending on how you have your keys mapped.

Update: Some readers from digg have pointed out that you can also switch bash to use vi style editing keys. This article covers the default bash keys, but I’ll write another one to cover vi style keys as well… great suggestions! Subscribe to the feed for updates.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/16/06

Comments (59)

  1. deian

    Ctrl + W erase word before cursor …. lot of fun stuff with ioctl

  2. The Geek

    deian,

    Thanks for the tip, I’ve added that one to the list.

  3. Hank

    I knew all but ^W and ^U. Probably won’t use those. Thanks though.

  4. MP

    A couple of interesting shortcuts are Ctrl + K to cut (as in delete and copy) all text in front of the cursor followed by Ctrl + Y to paste it.

    It’s quite useful to have a complex gcc command in a “clipboard” so one can avoid the tedious task of using the up arrow to sift through all the commands used for running and debugging the programing just compiled, as well as intermediate commands such as ls and cd. Ready to re-compile? Just Ctrl + Y, Enter.

  5. Mike

    You might want to mention that these are are made possible by ‘readline’ which lets you use emacs (or optionally vi) style editing keys.

    A fun editing trick that sometimes freaks people out when they see you doing it is “twiddle”. With Ctrl+T you can swap the position of two characters. Then there is “meta twiddle” (Esc+T) which swaps the positions of two words. Very cool.

  6. B

    Ctrl + K — Clears the line after cursor position.

  7. Emanuelez

    Ctrl +K erases everything after the cursor (Emacs style)

  8. The Geek

    Wow, I woke up from a nap and I’m on digg again… you guys are great, thanks for the tips.. I’ll update the article

  9. The Geek

    Mike,
    I just tried Ctrl+T… that is absolutely great!

  10. aoeuhtns

    alt+T has the same effect as Esc+T and is easier to reach;-)

    also, you forgot to mention Alt+B and Alt+F to go forward and back to the next word on the line

  11. Hugh

    esc . is the most useful: gives you the last argument of the last command

    thus

    % ls fred
    %cat (esc .) produces cat fred

    really good if ‘fred’ was generated by filename completion :-)

  12. The Geek

    Hugh,
    That’s a great one… that command makes a lot more sense in context!

  13. aoeuhtns

    !$ is the same as Esc .

    …and it’s also closer to reach:-)

  14. Kirk Badger

    Thanks a million.
    What a great Linux timesaver.

  15. Pedro

    Ctrl-_ is undo – useful if you type ctrl-k when you meant ctrl-u, etc

  16. cyndar

    You should also consider bash-completion. Essential for CLI junkies.

    bash-completion provides a set of completion functions for many commands, e.g. cd (will only list directory names in pwd), ls – (will list all possible command line options for ls), acroread (will list only .pdf files in pwd). There’s a huge list. It’s in the fedora extras for all fedora users. Also available as a tarball and probably packages available for other distros as well.

  17. phil

    Great article, but as I was going to say, try it with vi settings – I love it. I think it’s:

    set -o vi

    in .profile. You can check mine out here, I keep them on the home server to wget them to any work system I need to shell on:

    http://phil.cryer.us/code/dotfiles/profile
    http://phil.cryer.us/code/dotfiles/bashrc

    While we’re at it, turn on syntax highlighting and other nice features of vim:
    http://phil.cryer.us/code/dotfiles/vimrc

    HTH

  18. Ben

    I find ctrl+u is most helpful if you are typing in a password field at login and you want to start again because you have made a mistake.

    Eg: (this is a login prompt at tty1)

    hostname login: USERNAME
    Password: PASSWRO ctrl+u PASSWORD

    Why don’t you just press backspace you may ask? Well, sometime you don’t know how many keys you pressed and don’t know how far to go back :)

  19. MP

    “You might want to mention that these are are made possible by ‘readline’ which lets you use emacs (or optionally vi) style editing keys.”

    I wasn’t aware of that. Thanks for the info! :-)

  20. max

    ‘bind -P | less’
    The complete list!
    You can also create your own:
    ‘$ bind “\C-b”:backward-word’ to go backward one word at time with ‘control-b’
    You can also launch apps with them:
    $ bind -x ‘”\C-e”‘:mozilla.

  21. jake

    >>esc . is the most useful: gives you the last argument of the last command
    I find esc too far from typing area and pressing it hard. Thats why I use Alt+. instead of this.
    Alt+. gives you the last argument of the last command

  22. Zero_Dogg

    Ctrl+C does *NOT* kill the running app, it simply sends SIGINT to it, which is completely different from actually sending SIGKILL.

  23. The Geek

    You are correct, however from a user’s point of view, it kills whatever is running.

  24. dhwjwq

    CTRL+D does send EOF

  25. Kurt Pfeifle

    @The Geek:

    Whatever the user’s point of view — he’s wrong thinking the process is *killed* by Ctrl+C.

    Why not educate the user (like you do with your list of keyboard shortcuts too) and enlighten him about the difference between SiGINT and SIGKILL

  26. The Geek

    Kurt,

    Sounds like a good topic for another article, actually. =)

  27. rcmc2020

    Tab completion will complete the name up to the point where it is no longer unique. If you press tab a second time, it will print a list of commands or files that are applicable.

    For example,
    $ ls
    will print out all commands that begin with ls. If you put a space after the ls, it will assume you are looking for a filename.

  28. B.Rain

    Ctrl + y – recalls the last thing removed by a Ctrl + u. Useful if you had a long command typed out, but before running it, you realize that you need to do something first.

    ^ls^vi – re-runs the previous command, replacing the first instance of the first string with the second string. So running this after running ls /home/user/file would run vi /home/usr/file. Very similar to the Esc . thing listed above.

    Esc ~ performs a user name completion. If you are changing somebody’s password, as long as you know enough characters to distinguish it from all the others, you can do “passwd der” Esc ~, and it will complete dereksmith01.

    Pressing Esc Esc should do a listing of the current directory while you are in the middle of typing a command, but I usually find you have to hit it three or four times to get it. I haven’t taken the time to figure out exactly how it differs from Tab Tab, but I think there are some differences.

    ! will run the command listed in the output of a history command. (Sorry if this is a repeat. I know I saw !$, but I didn’t notice this one.) For example

    history | tail -3
    481 aixpert -l low -n
    482 ls -l /etc/security/aixpert/core/
    483 vi /etc/security/aixpert/core/secaixpert.xml
    !481 will run the aixpert command. After that is over, you can type !483, and it will vi the output.

    Enjoy

  29. Pedant

    Actually
    CTRL-C = SIGINT
    kill = SIGTERM
    kill -9 = SIGKILL

  30. nogg3r5

    I just tried these out in Cygwin, they work there too.

  31. Jason

    Addition to CTRL+Z:
    While fg will resume the process, bg will continue running the process in the background. It’s the equivalent of ending a a command with “&”.

  32. Rowan

    As others have already mentioned, some of these shortcuts are actually libreadline shortcuts. Recently, I published a more extensive summary of readline keyboard shortcuts myself.

  33. hillbilly

    Hi.

    Nice forum design. Okay, I need your advice.
    So, I wanna make online-shop, and I am looking for site template.
    Can you suggest some online store or other resource where I can find many site templates?

    It would be better if it will be free:)
    I think many of us have personal sites, do you design it yourself?

    Thx, Bill.

  34. The Geek

    For good site templates, check out the WordPress theme sites, or OSWD.

  35. PLJ

    Great list, thanx. Thanx to Rowan 2, for his list.

    For professional online shop systems do a search on sf.net. Then choose one out of many :P
    If you dont care learning a bit I’d recommend Typo3. You don’t get it more professional then that for free.

  36. Skerit

    Quite useful, it is! :)

    Thanks

  37. Erik

    FWIW The shortcuts also apply to Mac OSX Terminal.

    However “ALT” is a PCizm and is mapped to “send esc before every key I press. Thus ESC .(dot) can also me done with ALT+.(dot) and ALT+F can also be done with ESC F. Make sense? Probably not.

    To make this work well in Terminal check the “map option as meta key” perference. Then you can use Option+ for all ALT+ shortcuts and also meta keys like esc .

  38. Gareth

    Erik – those hints for the OSX terminal are exactly what I’ve being looking for. Since leaving the world of windows I’ve been missing alt+. :-)

    Thanks!!

  39. Art

    Ctrl + P: go to previous command in history
    Ctrl + N: go to next command in history
    Up and Down arrows do same
    Ctrl + R: search history backwards

  40. Peteris Krumins

    I wrote two detailed posts on both command line editing modes there are in Bash — vi editing mode and emacs editing mode:

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

    and

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

  41. hdh

    Ctrl+\ sends SIGKILL, which cannot be ignored. Thus it is more effective in ending programs.

  42. Srikanth

    I find this shortcut very useful:

    ESC + . (dot) = Repeats the last command’s last argument

    For example:

    mkdir ~/bin/blah/blew
    cd ESC+.

  43. rizme

    if you want to color your shell try this

    export PS1=”\e[3;35\m]/u@ /h /w “

  44. appleguy

    These are great tips – thanks everyone! And thanks to Rowan for the compiled list.

    For anybody using iTerm instead of Terminal, the setting to use “Option” key as “Meta” is buried, but still there.

    Bookmarks > Manage Profiles
    Keyboard profiles > xterm (OS X)
    Option Key as “+Esc”

  45. tmg

    Another to have handy is using PgUp and PgDn to cycle through command history matching what is left of the cursor.

    m[PgUp] may show the recent mv, mount, or mkdir commands you have issued.

  46. Khaled

    set -o vi
    gives you vim power over your bash commands ;) .. kinda cool.

  47. Villaroko

    YouTube, Google Inc’s popular video sharing site,
    http://twitter.com/qoclick
    Villa (qoclick) on Twitter
    is giving away tools that let Web developers tap
    the underlying database functions of

  48. Allan

    AWESOME!!!!!!! Thanks to you now I know that I am a stupid idot … I was using Ubuntu/Debian now for several years (casual basis), always annoyed because of the crappy character navigation BUT NEVER ASKED MYSELF IF THERE ARE MORE INTELLIGENT ALTERNATIVES AS KNOWN IN OTHER HIGHLEVEL TEXTEDITORS …. omg!!

    Thanks for this article!!!

  49. Me

    “let’s” = “let us”

  50. creativesumant

    How can I reliably paste from the X Primary buffer? Shift+Insert works but only as long as there’s no content in the X Clipboard buffer.

  51. din

    Depending on which terminal you use:

    Ctrl + Shift + c = Copy selected text from the terminal
    Ctrl + Shift + v = Paste clipboard into terminal

  52. jerrit

    I think ctrl+\ sends SIGQUIT.

  53. NM

    Nice to know that CTRL C is for a *graceful* exit. I’d always been diffident about using it because I thought it was a dirty kill.
    Thanks for the list :-)

  54. Grant

    Another handy one that nobody has mentioned is Ctrl + O. You might have to play with this one for a while before you work out how incredibly handy it is, but here’s an overview …

    You use Ctrl-O after searching back in history for a command you want to run again. Pressing Ctrl-O will execute the command and then bring up the command that came immediately after it in the history so you can execute that one by pressing Ctrl-O too. When you run a command with Ctrl-O is is (of course) added to your command history so you can just keep pressing it and replaying commands for ever.

  55. reggae

    What do it: Ctrl+Y and Ctrl+S ?

  56. Gajju

    It’s so funy…….

  57. Srikanth

    Ctrl + U doesn’t only delete line before the cursor but also copies it into the buffer, You can use Ctrl + Y to paste it.
    Ctrl + Shift + D logs out of any session(s).

  58. FlieseeGait

    Between me and my husband we’ve owned more MP3 players over the years than I can count, including Sansas, iRivers, iPods (classic & touch), the Ibiza Rhapsody, etc.
    But, the last few years I’ve settled down to one line of players. Why? Because I was happy to discover how well-designed and fun to use the underappreciated (and widely mocked) Zunes are.

  59. LeFarfadet

    one I find amusing is :
    CTRL + X CTRL+E

    you have to type this after you typed one command :
    ls /home/user1 [CTRL +X] [CTRL +E]

    The command will be automatically copied in your favorite EDITOR (add “EDITOR=vi” or emacs or whatever you like in your ~/.bashrc). You will then be able to modify, copy, add other lines just like in a file with a shu bang on top.

    When you close the editor, saving or not, the commands are executed. (even if you close vi with :q!)

Enter Your Email Here to Get Access for Free:

Go check your email!