The command line is all about efficiency, and mastering keyboard shortcuts for command line 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.
Why Learn Keyboard Tricks?
In this chapter, we’ll cover:
– Command-line editing shortcuts.
– Using command history effectively.
– Keyboard tricks for faster navigation and editing.
Why Learn Keyboard Shortcuts for Command Line?
Keyboard shortcuts for command line boost terminal efficiency, speeding up editing, navigation, and command recall. This chapter builds on Chapters 3 ($ history), 5 ($ source), and 7 (quoting/escaping), teaching beginners to work smarter in the Linux shell.
Command-Line Editing
Essential Shortcuts for Editing
Use these keyboard shortcuts for command line to edit commands efficiently.
Moving the Cursor
- Ctrl + A: Move to line beginning.
- Ctrl + E: Move to line end.
- Alt + B: Move back one word.
- Alt + F: Move forward one word.
- Ctrl + B: Move back one character.
- Ctrl + F: Move forward one character.
Editing Text
- Ctrl + D: Delete character under cursor.
- Ctrl + H: Delete character before cursor (like Backspace).
- Ctrl + W: Delete word before cursor.
- Ctrl + K: Delete from cursor to line end.
- Ctrl + U: Delete from cursor to line beginning.
- Ctrl + T: Swap character under cursor with previous one.
Repeating Commands
- Ctrl + P: Recall previous command (like up arrow).
- Ctrl + N: Recall next command (like down arrow).
Using Command History
Boosting Terminal Efficiency with History
Leverage command history for faster workflows.
Viewing History
$ history
Output: 1 ls -l 2 cd Documents
Searching History
- Ctrl + R: Search history interactively; type to autocomplete.
$ !!: Repeat last command.
Output: Runs previous command (e.g.,cd Documents).$ !n: Repeat command numbern.
Output: Runs commandnfrom history.$ !string: Repeat recent command starting withstring.
Output: Runs command (e.g.,!ls→ls -l).
Editing a Previous Command
- Ctrl + X + E: Edit last command in editor (e.g.,
nano, see Chapter 4); save to run.
Keyboard Tricks for Faster Navigation
Speeding Up Terminal Tasks
Additional shortcuts enhance navigation.
Clearing the Screen
- Ctrl + L: Clear screen (like
$ clear).
Stopping a Command
- Ctrl + C: Stop running command.
Pausing and Resuming Output
- Ctrl + S: Pause command output.
- Ctrl + Q: Resume output.
Background and Foreground
- Ctrl + Z: Pause command, move to background.
$ fg: Bring recent background job to foreground.$ bg: Resume paused job in background.
Customizing Keyboard Shortcuts
Personalizing Your Shell
Edit .bashrc to customize shortcuts:
$ bind '"\C-p": "echo Hello, Linux!"'
Reload:
$ source ~/.bashrc
Output: Ctrl + P now prints Hello, Linux!.
Custom Keybindings
Advanced Terminal Efficiency
Use bind for custom shortcuts and tmux for session management.
Install:
$ sudo apt install tmux
Usage:
$ bind '"\C-f": "ls -la"'
$ tmux new -s mysession
Output: Ctrl + F runs ls -la; tmux opens a session named mysession.
Note: Compared to history (basic command recall, see Chapter 5), tmux manages multiple terminal sessions, enhancing workflow organization for beginners.
Installation Note: tmux may be in Debian 12 repositories, but verify with apt. See Debian APT.
Learn more in Chapter 26: Learning tmux.
Glossary of Commands, Tools, and Shortcuts
Reference: For detailed command documentation, visit Linux Manpages. For package installation, search on Debian APT.
| Command/Tool/Shortcut | Description |
|---|---|
| history | Lists previously executed commands. |
| clear | Clears the terminal screen. |
| fg | Brings background job to foreground. |
| bg | Resumes paused job in background. |
| bind | Customizes keyboard shortcuts. |
| source | Reloads shell configuration. |
| tmux | Manages multiple terminal sessions. |
| nano | Simple text editor. |
| vi | Powerful text editor. |
| Ctrl + A | Moves cursor to line beginning. |
| Ctrl + E | Moves cursor to line end. |
| Alt + B | Moves cursor back one word. |
| Alt + F | Moves cursor forward one word. |
| Ctrl + B | Moves cursor back one character. |
| Ctrl + F | Moves cursor forward one character. |
| Ctrl + D | Deletes character under cursor. |
| Ctrl + H | Deletes character before cursor. |
| Ctrl + W | Deletes word before cursor. |
| Ctrl + K | Deletes from cursor to line end. |
| Ctrl + U | Deletes from cursor to line beginning. |
| Ctrl + T | Swaps character under cursor with previous. |
| Ctrl + P | Recalls previous command. |
| Ctrl + N | Recalls next command. |
| Ctrl + R | Searches command history interactively. |
| Ctrl + L | Clears terminal screen. |
| Ctrl + C | Stops running command. |
| Ctrl + S | Pauses command output. |
| Ctrl + Q | Resumes paused output. |
| Ctrl + Z | Pauses command, moves to background. |
| Ctrl + X + E | Edits last command in text editor. |
Practice Keyboard Shortcuts for Command Line
Test your skills:
- Use
Ctrl + AandCtrl + Eto move cursor in a command. - Use
Ctrl + Rto search for a previous command. - Use
Ctrl + Kto delete to line end. - Use
Ctrl + Zto pause a command, then$ fgto resume.
Conclusion
You’ve mastered keyboard shortcuts for command line, from Ctrl + A to tmux, boosting terminal efficiency. Practice these to navigate the shell faster. Next, we’ll explore more advanced topics! (See Chapters 3–7 for history, file management, and shell interpretation.)
That’s it for Chapter 8 Keyboard Shortcuts for Command Line ! 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.