Navigating in the Bash Shell

Posted by

Spending a lot time in the bash terminal asks for navigation optimizations while editing commands. This article will take you through my favourites. At the end I have some interesting links to more information to the topic.

The below picture is a perfect visual reminder for me that I have found on the internet:

My Favourites

  • Forgot to type sudo at the beginning? Ctrl+u + type sudo + Ctrl+y
  • Already hit enter and forgot sudo? : sudo !!
Ctrl + u & sudo & Ctrl + yCut all before cursor. Type sudo. Paste back cut text (yank)
sudo !!Type sudo !! where !! repeats the last run command.

Move cursor

Ctrl + aGo to the beginning of the line (Home)
Ctrl + eGo to the End of the line (End)
Alt + bBack (left) one word
Alt + fForward (right) one word
Ctrl + fForward one character
Ctrl + bBackward one character
Ctrl + xxToggle between the start of line and current cursor position

Edit

Ctrl + uCut the line before the cursor position
Alt + DelDelete the Word before the cursor
Alt + dDelete the Word after the cursor
Ctrl + dDelete character under the cursor
Ctrl + hDelete character before the cursor (backspace)
Ctrl + wCut the Word before the cursor to the clipboard
Ctrl + kCut the Line after the cursor to the clipboard
Alt + tSwap current word with previous
Ctrl + tSwap the last two characters before the cursor (typo)
Esc + tSwap the last two words before the cursor.
Ctrl + yPaste the last thing to be cut (yank)
Alt + uUPPER capitalize every character from the cursor to the end of the current word.
Alt + lLower the case of every character from the cursor to the end of the current word.
Alt + cCapitalize the character under the cursor and move to the end of the word.
Alt + rCancel the changes and put back the line as it was in the history (revert)
Сtrl + _Undo

History

Ctrl + rRecall the last command including the specified character(s)(equivalent to : vim ~/.bash_history).
Ctrl + pPrevious command in history (i.e. walk back through the command history)
Ctrl + nNext command in history (i.e. walk forward through the command history)
Ctrl + sGo back to the next most recent command.
Ctrl + oExecute the command found via Ctrl+r or Ctrl+s
Ctrl + gEscape from history searching mode
Alt + .Use the last word of the previous command

You notice that some of the commands require the ALT key. There is no ALT key on a Mac keyboard. But there is an Option key. To use the Option key with the functionality of the ALT key there need to be some changes to the settings of the Terminal you use. Let me show below for the default Mac Terminal and also the iTerm2 Terminal. The ALT key is also referred to as Meta key or as ESC+ key.

For the default Terminal open the preferences and follow the steps:

  • Select the profile you are using.
  • Select Keyboard on the right
  • Select “Use Option as Meta key”

For iTerm2 the steps are similar. Open the preferences and follow the steps:

  • Select the profiles menu.
  • Select the default profile you use.
  • Click on the Keys configuration on the right.
  • Select for both Option keys the Esc+ option on the right. (Here selecting Meta does not get the result we are looking for.

Sources:

Bash Shortcuts For Maximum Productivity – Skorks
https://skorks.com/2009/09/bash-shortcuts-for-maximum-productivity/

fliptheweb/bash-shortcuts-cheat-sheet: Useful shortcuts for bash/zsh
https://github.com/fliptheweb/bash-shortcuts-cheat-sheet

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *