Popular Keyboard Shortcuts for the GNU Bash Shell

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,263
Reaction score
2,432
Credits
3,941
The GNU Bash shell, an acronym for GNU Bourne-Again Shell, is a Unix shell and command-line interpreter used in most Linux distributions and macOS. It has become the de facto standard for shell scripting and is widely used by developers and system administrators.

One of my favorite reasons for using Linux and the command line in particular is learning keyboard shortcuts, which can help to improve your efficiency and speed when using the Bash shell. This article will discuss some popular keyboard shortcuts in the GNU Bash shell to help you along the way.

  1. Navigation Shortcuts
The following keyboard shortcuts can help you move through text and command history:

  • Ctrl + a: Move the cursor to the beginning of the line
  • Ctrl + e: Move the cursor to the end of the line
  • Alt + b: Move the cursor backward one word
  • Alt + f: Move the cursor forward one word
  • Ctrl + xx: Toggle between the current cursor position and the beginning of the line
  1. Editing Shortcuts
These editing shortcuts will help you modify your commands without having to reach for the arrow keys or delete key:

  • Ctrl + w: Cut the word before the cursor
  • Alt + d: Cut the word after the cursor
  • Ctrl + k: Cut the text from the cursor to the end of the line
  • Ctrl + u: Cut the text from the cursor to the beginning of the line
  • Ctrl + y: Paste the last cut text
  • Alt + y: Paste the second most recent cut text
  • Ctrl + _: Undo the last change
  1. Command History Shortcuts
These shortcuts make it easy to navigate, search, and reuse your command history:

  • Ctrl + p: Show the previous command from history
  • Ctrl + n: Show the next command from history
  • Alt + .: Insert the last argument of the previous command
  • Ctrl + r: Search the command history backward (use Ctrl + s to search forward)
  • Ctrl + g: Exit command history search mode
  • Ctrl + o: Execute the found command from history and show the next command
  • Ctrl + l: Clear the screen (same as the 'clear' command)
  1. Process Control Shortcuts
The following shortcuts will help you control processes without having to type out the full command:

  • Ctrl + c: Send the SIGINT signal to the current foreground process, usually resulting in termination
  • Ctrl + z: Send the SIGTSTP signal to the current foreground process, suspending its execution
  • Ctrl + d: Send an EOF (End of File) to the current process, usually resulting in termination or logout
  • Ctrl + s: Pause the output to the terminal (useful when scrolling through large amounts of output)
  • Ctrl + q: Resume output to the terminal after pausing with Ctrl + s
  • bg: Resume a suspended process in the background
  • fg: Resume a suspended process in the foreground
  1. Miscellaneous Shortcuts
Lastly, here are some additional shortcuts that can come in handy:

  • Ctrl + t: Transpose (swap) the characters before and under the cursor
  • Alt + t: Transpose (swap) the words before and under the cursor
  • Alt + u: Uppercase the word from the cursor to the end of the word
  • Alt + l: Lowercase the word from the cursor to the
Finishing up & important notes

After some feedback from users on the r/linux subreddit, I'd like to add that these are indeed considered readline commands/shortcuts that work while bash is in the default 'emacs' mode. These likely won't work if you're in vi mode. For more info, type help at your bash prompt or for even more info take a look at the bash man page by typing man bash. Further, you could check out the readline man page.

Some of these shortcuts may work in other popular shells derived from bash like ksh and zsh, but you'd have to check their corresponding help/man pages to be sure.
 
Last edited:


It's all handy, but the following in particular

  • Ctrl + s: Pause the output to the terminal (useful when scrolling through large amounts of output)
  • Ctrl + q: Resume output to the terminal after pausing with Ctrl + s

... may prove very useful to me.

Ta for sharing ;)

Wiz
 
This is a very personal comment that may not be applicable to others:

I find simple lists like these much easier to use and learn from. @KGIII offers his fine Linux Tips website. It tends to "spoon feed" hints one command at a time. Lists like this work better for me, but others may prefer the Linux Tips here:

https://linux-tips.us
 
This is a very personal comment that may not be applicable to others:

I find simple lists like these much easier to use and learn from. @KGIII offers his fine Linux Tips website. It tends to "spoon feed" hints one command at a time. Lists like this work better for me, but others may prefer the Linux Tips here:

https://linux-tips.us
Thanks for the advice!
 
Just a note: CTRL+L is not the same as the clear command. CTRL+L is not a command, so it won’t overwrite the last exit code ($?), but clear will, with a 0, as not likely will succeed every time it’s invoked.

Probably a very minor detail, but if you’re clearing the screen while debugging some error codes that may be a difference worth noting.
 
Just a note: CTRL+L is not the same as the clear command. CTRL+L is not a command, so it won’t overwrite the last exit code ($?), but clear will, with a 0, as not likely will succeed every time it’s invoked.

Probably a very minor detail, but if you’re clearing the screen while debugging some error codes that may be a difference worth noting.

Yes, CTRL-L simply scrolls your current position on the screen to the top of the screen so that previous output is not lost. You can simply scroll up to see what that previous output is i.e. it's a non-destructive way to clear the screen.

FWIW, I have a set an alias CLS
Code:
alias cls='clear'

which harks back to my BASIC programming days :)
 

Staff online

Members online


Top