Use Bash more efficiently: Keyboard Shortcuts

3 minute read

This is a post on Bash Keyboard Shortcuts and if used regularly these passwords will surely lighten your tasks on the Bash shell 🙂 .

But before beginning, you need to understand that, your Bash shell can either run in emacs mode or the vi mode and the shortcuts are different for each mode. Mostly the default mode is emacs and this post assumes this. But you can check your mode as follows:

$ shopt -o | grep emacs
$ shopt -o | grep vi

Now, if emacs mode is off and vi mode is on, just toggle these as follows(By the way I already taught you how to work with shopt in a previous post):

$ shopt -u -o vi
$ shopt -s -o emacs

So having said that lets get started:

  • Clear the screen: CTRL + L
  • Swap the the last two words before the cursor: CTRL + T
    This shortcut can be used to correct a typo, For Example: say you typed grpe instead of grep, so press Ctrl + T at this position and your typo is corrected.
  • Swap the last two words before the cursor: ESC + T
    So say, you forgot whether in grep the expression to search for comes first or the filename, so you may use this shortcut like this:</p>
    $ grep file pattern
    $ grep: pattern: No such file or directory

    Now, use CTRL + P(or the up arrow key) to go to the previous command and press ESC + T and the words are swapped and it works now.

    $ grep pattern file
  • Cut the word just before the cursor: CTRL + W.
    $ abcd efgh ijkl mnop qrst
    $ abcd efgh ijkl mnop (CTRL + W pressed once)
    $ abcd efgh ijkl (CTRL + W pressed twice)
  • Cut everything after the cursor: CTRL + K or ALT + D
    $ abcdefghijkl (cursor is at g and CTRL + K pressed.)
    $ abcdef
  • Cut the whole line before the cursor: CTRL + U
    $ abcd efgh ijkl mnop qrstu(Cursor here CTRL + U pressed.)
    $ (Cursor here)
  • So how do you Paste what you cut in the last three shortcuts?? Not through CTRL + V but: CTRL + Y – This by the way is called yanking, therefore the Y.
  • Now, what if I told you that you can UNDO what you typed/edited/etc on the line in Bash? Great isn’t it: It’s easy just use: CTRL + SHIFT + – or better said as CTRL + _ . Just try it yourself it’s fun 🙂 .
  • Toggle between the start of line and current cursor position: CTRL + XX.
  • Convert all characters from the cursor to the end of the current word to uppercase: ALT + U
  • Convert all characters from the cursor to the end of the current word to lowercase: ALT + L
  • Of course there is the awesome shortcut for auto-completion of words/commands:  TAB.
  • And, don’t forget to press tab twice to see all possible hints.
  • Move to the beginning of the next word in reverse order on the line: ALT + B.
  • Move to the beginning of the current line: CTRL + A or HOME key.
  • Move to the end of the current line: CTRL + E or END key.
  • Move back one character: CTRL + B or LEFT arrow key.
  • Move forward one character: CTRL + F or RIGHT arrow key.

So, these are all the cool shortcuts I wanted to tell you about. I hope you liked few of these and will bring into practice using these for more efficiency when working on the Bash shell. Thanks for reading. Comment, if you have any.




Leave a Comment