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.
|ctrl + a
|Move to the start of the command line
|ctrl + e
|Move to the end of the command line
|ctrl + b
|Move one character to the left
|ctrl + f
|Move one character to the right
|alt + f
|Move one word to the right
|alt + b
|Move one word to the left
|ctrl + xx
|Switch between the current and the first position of the cursor
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.
|ctrl + x,e
|Edit command in editor
|ctrl + d
|Delete a character forward or exit the shell if the line is empty.
|ctrl + h
|Delete a character to the left. Equivalent to backspace
|alt + d
|Delete a word forward from the cursor.
|ctrl + w
|Delete a word backward from the cursor. Equivalent to alt + backspace
|ctrl + u
|Cut from cursor to start of line.
|ctrl + k
|Cut from cursor to end of line.
|ctrl + y
|Paste the last cut text.
|alt + u
|Capitalize every character from cursor to end of word.
|alt + l
|Lowercase every character from cursor to end of word.
|alt + c
|Capitalize the character under cursor and move to end of word.
|alt + t
|Swap current word with the previous word.
|Complete the current word or list possible completions.
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.
|ctrl + c
|Interrupt (kill) the current foreground process running in bash. Sends the SIGINT signal to the process, requesting termination.
|ctrl + z
|Suspend (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 + s
|Stop all output to the screen. Useful for halting verbose commands without stopping the command itself (unlike Ctrl+C).
|ctrl + q
|Resume output to the screen after stopping it with Ctrl+S.
|ctrl + l
|Clear the screen. Equivalent to running the clear command.
|ctrl + d
|Close the bash shell by sending an EOF (End-of-file) marker to bash, prompting exit. Similar to running the exit command.
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.
|ctrl + r
|Incremental 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 + s
|Incremental forward search of bash history. Similar to Ctrl+R but searches forward.
|alt + p
|Non-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 + n
|Non-incremental forward search of bash history. Similar to Alt+P but searches forward.
|ctrl + p or up arrow
|Move to the previous command in history.
|ctrl + n or down arrow
|Move to the next command in history.
|ctrl + o
|Run the current command and re-enter it in history.
|ctrl + g
|Exit the history search mode without running the command.
|Run the last command in history.
|Run the last command in history except its first word.
|Display what ! substitutes.
|Run the most recent command in history that begins with x.
|Display 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.
|Replace 123 with abc in the previous command and run it.
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.
|GNU Bash Manual