Asaduzzaman Pavel

Bash Shortcut: A Guide to Keyboard Shortcuts for Bash Terminal

Bash - The Bourne-Again Shell

GNU Bash is a popular command-line shell that is used by many Linux and MacOS users. It allows users to execute commands, run scripts, and interact with the system. However, typing commands in bash can be tedious and time-consuming, especially if you have to repeat them often or make small changes. That's why bash provides a number of keyboard shortcuts that can help you save time, improve efficiency, and enhance your command-line experience.

In this article, we will learn some of the most useful bash keyboard shortcuts that you can use for navigation, editing, control, and history.

Navigation shortcuts allow you to move the cursor around the current line while typing a command. They can help you quickly reach the beginning or end of the line, move forward or backward by one character or one word, or switch between different positions. Here are some of the navigation shortcuts that you can use.

commanddescription
ctrl + aMove to the start of the command line
ctrl + eMove to the end of the command line
ctrl + bMove one character to the left
ctrl + fMove one character to the right
alt + fMove one word to the right
alt + bMove one word to the left
ctrl + xxSwitch between the current and the first position of the cursor

Editing Shortcuts

Editing shortcuts allow you to modify the command that you are typing. They can help you delete, undo, cut, paste, complete, or capitalize characters, words, or lines. Here are some of the editing shortcuts that you can use.

ShortcutDescription
ctrl + x,eEdit command in editor
ctrl + dDelete a character forward or exit the shell if the line is empty.
ctrl + hDelete a character to the left. Equivalent to backspace
alt + dDelete a word forward from the cursor.
ctrl + wDelete a word backward from the cursor. Equivalent to alt + backspace
ctrl + uCut from cursor to start of line.
ctrl + kCut from cursor to end of line.
ctrl + yPaste the last cut text.
alt + uCapitalize every character from cursor to end of word.
alt + lLowercase every character from cursor to end of word.
alt + cCapitalize the character under cursor and move to end of word.
alt + tSwap current word with the previous word.
tabComplete the current word or list possible completions.

Control Shortcuts

Control shortcuts allow you to manage the processes that are running in bash. They can help you stop, resume, kill, or exit processes. Here are some of the control shortcuts that you can use.

ShortcutDescription
ctrl + cInterrupt (kill) the current foreground process running in bash. Sends the SIGINT signal to the process, requesting termination.
ctrl + zSuspend (pause) the current foreground process running in bash. Sends the SIGTSTP signal to the process. To resume the process later, use fg process_name command.
ctrl + sStop all output to the screen. Useful for halting verbose commands without stopping the command itself (unlike Ctrl+C).
ctrl + qResume output to the screen after stopping it with Ctrl+S.
ctrl + lClear the screen. Equivalent to running the clear command.
ctrl + dClose the bash shell by sending an EOF (End-of-file) marker to bash, prompting exit. Similar to running the exit command.

History Shortcuts

History shortcuts allow you to access and reuse previous commands that you have typed in bash. They can help you search, recall, edit, or run commands from your history. Here are some of the history shortcuts that you can use.

ShortcutDescription
ctrl + rIncremental reverse search of bash history. Type a part of a command to display the most recent matching command. Press Ctrl+R again for older matching commands. Press Enter to run or Esc to edit before running.
ctrl + sIncremental forward search of bash history. Similar to Ctrl+R but searches forward.
alt + pNon-incremental reverse search of bash history. Type a part of a command and press Alt+P to see older matching commands. Press Enter to run or Esc to edit before running.
alt + nNon-incremental forward search of bash history. Similar to Alt+P but searches forward.
ctrl + p or up arrowMove to the previous command in history.
ctrl + n or down arrowMove to the next command in history.
ctrl + oRun the current command and re-enter it in history.
ctrl + gExit the history search mode without running the command.
!!Run the last command in history.
!*Run the last command in history except its first word.
!:pDisplay what ! substitutes.
!xRun the most recent command in history that begins with x.
!x:pDisplay the x command and add it as the most recent command in history.
!$Substitute the last argument of the previous command in the current command.
!^Substitute the first argument of the previous command in the current command.
^123^abcReplace 123 with abc in the previous command and run it.

Wrapping Up

GNU Bash keyboard shortcuts are powerful and convenient tools that can help you improve your productivity and efficiency when working with the command-line shell. By using these shortcuts, you can save time and have more fun with bash. We hope that this article and cheat sheet have helped you learn and use these shortcuts effectively.

ResourceLink
GNU Bashhttps://www.gnu.org/software/bash/
GNU Bash Manualhttps://www.gnu.org/software/bash/manual/bash.html
Asaduzzaman Pavel

About the Author

Asaduzzaman Pavel is a Software Engineer who actually enjoys the friction of a well-architected system. He has over 15 years of experience building high-performance backends and infrastructure that can actually handle the real-world chaos of scale.

Currently looking for new opportunities to build something amazing.