Chapter 8: Keyboard Shortcuts

In this chapter, we’ll cover: 
- Command-line editing shortcuts. 
- Using command history effectively. 
- Keyboard tricks for faster navigation and editing.


Why Learn Keyboard Tricks?

The command line is all about efficiency, and mastering keyboard shortcuts can dramatically speed up your workflow. Whether you’re fixing a typo, recalling a previous command, or navigating a long command, these tricks will make your life easier.


1. Command-Line Editing

The shell provides several shortcuts for editing commands. Here are the most useful ones:

Moving the Cursor

  • 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 back one word.
  • Alt + F: Move the cursor forward one word.
  • Ctrl + B: Move the cursor back one character.
  • Ctrl + F: Move the cursor forward one character.

Editing Text

  • Ctrl + D: Delete the character under the cursor.
  • Ctrl + H: Delete the character before the cursor (same as Backspace).
  • Ctrl + W: Delete the word before the cursor.
  • Ctrl + K: Delete everything from the cursor to the end of the line.
  • Ctrl + U: Delete everything from the cursor to the beginning of the line.
  • Ctrl + T: Swap the character under the cursor with the one before it.

Repeating Commands

  • Ctrl + P: Recall the previous command (same as the up arrow).
  • Ctrl + N: Recall the next command (same as the down arrow).

2. Using Command History

The shell keeps a history of the commands you’ve typed. Here’s how to use it effectively:

Viewing History

Use the history command to see a list of previously executed commands.

$ history

Searching History

  • Ctrl + R: Search your command history interactively. Start typing part of the command, and the shell will autocomplete it.
  • !!: Repeat the last command.
  • !n: Repeat the command with history number n.
  • !string: Repeat the most recent command starting with string.

Editing a Previous Command

  • Ctrl + X + E: Open the last command in your default text editor (e.g., nano or vi). Save and exit to run the edited command.

3. Keyboard Tricks for Faster Navigation

Here are some additional tricks to speed up your workflow:

Clearing the Screen

  • Ctrl + L: Clear the terminal screen (same as the clear command).

Stopping a Command

  • Ctrl + C: Stop the currently running command.

Pausing and Resuming Output

  • Ctrl + S: Pause command output (useful for long-running commands).
  • Ctrl + Q: Resume command output.

Background and Foreground

  • Ctrl + Z: Pause the current command and move it to the background.
  • fg: Bring the most recent background job to the foreground.
  • bg: Resume a paused job in the background.

4. Customizing Keyboard Shortcuts

You can customize keyboard shortcuts by editing your shell configuration file (e.g., .bashrc). For example, to map Ctrl + P to something else:

$ bind '"\C-p": "echo Hello, Linux!"'

Add this line to your .bashrc and reload it with source ~/.bashrc.


Extra: Custom Keybindings

Mastering keyboard shortcuts can significantly improve your terminal efficiency. Custom keybindings using bind allow you to create shortcuts for frequently used commands, while tmux enables you to manage multiple sessions and windows within a single terminal. 

By combining custom shell keybindings with tmux’s session management, you can reduce the time spent on repetitive tasks and maintain a more organized workflow.

# Install tmux
$ sudo apt install tmux

# Example usage:
$ bind '"\C-f": "ls -la"'
$ tmux new -s mysession

We will discuss tmux in greater details in Chapter 26 - Learning tmux


Practice Time!

Let’s put your new skills to the test: 
1. Use Ctrl + Aand Ctrl + E to move the cursor to the beginning and end of a command. 
2. Use Ctrl + R to search for a previous command. 
3. Use Ctrl + K to delete everything from the cursor to the end of the line. 
4. Use Ctrl + Z to pause a command and fg to bring it back to the foreground.


That’s it for Chapter 8! You’ve now learned how to work more efficiently on the command line using advanced keyboard tricks. In the next chapter, we’ll dive into permissions—understanding file permissions and ownership.


Prev: Chapter 7 | Next: Chapter 9