The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal)

The bash shell comes packed with a wide variety of keyboard shortcuts we can put to use.

These will work in bash on all operating system. Some of them might not work if you’re accessing bash remotely through an SSH or telnet session, depending on how you have your keys mapped.

Managing processes

Following key maps can be used to handle processes inside Bash:

  • Ctrl+C: This kills the current process running in inside bash. This generates a SIGINT signal to the process, which is just a request – most processes will accept it, but some may ignore it.
  • Ctrl+Z: Suspend the current foreground process running in bash. This sends the SIGTSTP signal to the process.

  • Ctrl+D: Close the bash shell. This sends an EOF (End-of-file) marker to bash, and bash exits when it receives this marker. This is similar to running the exit command.

Controlling the Screen

The following shortcuts allow you to control what appears on the screen.

  • Clear the screen : Use Ctrl+L. This is similar to running the “clear” command but shorter.
  • Ctrl+S: This will stop all the output to the screen. This is useful when a command with lots of verbose output is run. This will not stop the command itself.
  • Ctrl+Q: Resume output to the screen after stopping it with Ctrl+S.

Deleting Text

Following shortcuts are used to quickly delete characters:

  • Ctrl+D or Delete: Delete the character under the cursor.
  • Ctrl+H or Backspace: Delete the character before the cursor.
  • Alt+D: Delete all characters after the cursor on the current line.

Fixing Typos

These shortcuts allow you to fix typos and undo your key presses.

  • Ctrl+T: Swap the last two characters before the cursor with each other. You can use this to quickly fix typos when you type two characters in the wrong order.
  • Alt+T: Swap the current word with the previous word.
  • Ctrl+_: Undo your last key press. You can repeat this to undo multiple times.

Tab Completion

Tab completion is an important feature. While typing a file, directory, or command name, press Tab and bash will automatically complete what you’re typing, if possible. If not, bash will show you various possible matches and you can continue typing and pressing Tab to finish typing.

For example, if you any file, might be with a long name, long_file_name in /home/shubham/ and it’s the only file name starting with “lo” in that directory, you can type /home/shubham/r, press Tab, and bash will automatically fill in /home/shubham/long_file_name for you. If you have multiple files or directories starting with “lo”, bash will inform you of your possibilities.

You can start typing one of them and press “Tab” to continue. Here is an example:

Moving the Cursor

Use the following shortcuts to quickly move the cursor around the current line while typing a command.

  • Ctrl+A or Home: Go to the beginning of the line.
  • Ctrl+E or End: Go to the end of the line.
  • Alt+B: Go left (back) one word.
  • Ctrl+B: Go left (back) one character.
  • Alt+F: Go right (forward) one word.
  • Ctrl+F: Go right (forward) one character.
  • Ctrl+XX: Move between the beginning of the line and the current position of the cursor. This allows you to press Ctrl+XX to return to the start of the line, change something, and then press Ctrl+XX to go back to your original cursor position. To use this shortcut, hold the Ctrl key and tap the X key twice.

Leave a Reply