Cover image from My favourite bash shortcuts

My favourite bash shortcuts

October 30, 2018

I recently transitioned from using vi mode in bash to the default command line shortcuts. I had ignored them for a long time but after a brief period sitting and learning a few I no longer missed vi mode. Some of the key reasons that prompted me to change are:

  • vi mode does not have a visual prompt, leading to unecassary escape smashing
  • I only used the most basic motion command, eschewing a lot of the power of the vi commands
  • When ssh’ing into other boxes the switch in interface is jarring. This is exacerbated when switching between tmux window

I thought I’d outline here a few of my most used keyboard shortcuts:

Previous/Next command

Ctrl+P moves to your previous command, Ctrl+N moves in the other direction. After no time at all it becomes an incredibly natural keystroke

Search previous commands

Accessed by hitting Ctrl+R this mode will autofill the most recent and closest match for whatever keys you type. You can hit tab when you locate the command you want and then begin editing it

Edit command in editor

Hit and hold Ctrl while typeing Ctrl+X and then Ctrl+E. This will open whatever editor is set as $EDITOR and allow you to use this to edit the command

There is a fantastic list of navigation keyboard shortcuts on the following github page. It also includes a handy graphic which illustrates the effect of each of the commands. I regularly use Alt+F to move forward a word and Alt+B to move back a work Ctrl+E moves the cursor to the and of the line. Ctrl+A will bring you back to the start of the line. Lastly I find I use Ctrl+U to delete all content to the start of the line. https://github.com/fliptheweb/bash-shortcuts-cheat-sheet

Bonus

A lot of these shortcuts are shared by other applications. If you are in Google Chrome Dev Tools console most of the above commands are supported.