DEV Community

Beta Shorts
Beta Shorts

Posted on โ€ข Edited on

6 1

Why Every Dev Should Know These 20 Bash Keyboard Shortcuts

A few years ago, I wasted hours scrolling through command history, retyping long file paths, and fixing typos the slow way. Then I discovered Bash keyboard shortcuts.

After incorporating just a handful of these, my terminal speed doubled.

If you're still using Bash inefficiently, this guide will show you the shortcuts that experienced Linux users rely on dailyโ€”from basic navigation to power-user tricks.


1. Rerun the Last Command Instantly (!!)

Skill Level: Beginner

Instead of retyping a failed command with sudo, just use:

sudo !!
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Quickly retry failed commands without retyping everything.


2. Edit and Reuse the Last Argument (!$)

Skill Level: Intermediate

Instead of manually copying the last argument:

mv file1.txt /backup/
ls -l !$
Enter fullscreen mode Exit fullscreen mode

This expands to:

ls -l /backup/file1.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Saves keystrokes when working with long filenames.


3. Search Command History Like a Pro (Ctrl + R)

Skill Level: Intermediate

Stop pressing Up Arrow 20 timesโ€”use reverse search:

  1. Press Ctrl + R
  2. Start typing a previous command
  3. Press Enter to run it instantly

๐Ÿ”น Real-World Use Case: Instantly finds a complex command without scrolling through history.


4. Move to the Beginning or End of a Command (Ctrl + A / Ctrl + E)

Skill Level: Beginner

Stop holding Left Arrow to edit a command. Use:

  • Ctrl + A โ†’ Jump to the start
  • Ctrl + E โ†’ Jump to the end

๐Ÿ”น Real-World Use Case: Makes editing long commands much faster.


5. Delete a Whole Line (Ctrl + U)

Skill Level: Intermediate

Instead of holding Backspace, erase everything before the cursor instantly:

Ctrl + U
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Quickly clear mistakes without retyping everything.


6. Delete Everything After the Cursor (Ctrl + K)

Skill Level: Intermediate

echo "This is a test sentence"
Enter fullscreen mode Exit fullscreen mode

If the cursor is after "test", pressing Ctrl + K results in:

echo "This is a test"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Remove unnecessary text without deleting the whole command.


7. Swap Two Characters (Ctrl + T)

Skill Level: Advanced

Instead of manually fixing typos:

grpe file.txt  # Typo
Enter fullscreen mode Exit fullscreen mode

Press Ctrl + T:

grep file.txt  # Fixed
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Quick typo correction without retyping.


8. Cancel a Running Command (Ctrl + C)

Skill Level: Beginner

Stops a command immediately.

ping google.com
# Press Ctrl + C to stop
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Kills unresponsive processes instantly.


9. Suspend a Process (Ctrl + Z)

Skill Level: Advanced

Pause a running task instead of quitting it:

vim file.txt  # Press Ctrl + Z
Enter fullscreen mode Exit fullscreen mode

Resume later with:

fg
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Temporarily switch tasks without losing progress.


10. Run a Process in the Background (&)

Skill Level: Intermediate

long-running-task.sh &
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Keeps the terminal free while running background tasks.


11. Kill a Background Process (jobs + kill %)

Skill Level: Advanced

List background jobs:

jobs
Enter fullscreen mode Exit fullscreen mode

Kill job #2:

kill %2
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Manages multiple background tasks efficiently.


12. Clear the Terminal (Ctrl + L)

Skill Level: Beginner

Same as clear, but faster.

Ctrl + L
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Keeps your workspace clean.


13. Copy the Previous Command Without Running It (!:)

Skill Level: Advanced

Instead of retyping a long command, use !:p to print the last command without running it.

!:
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Preview a command before executing it again.


14. Replace Text in the Last Command (^old^new)

Skill Level: Intermediate

Instead of retyping a command to fix a mistake:

ls /ect
^ect^etc
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Fixes typos without manually editing the command.


15. Scroll Through Past Commands (Up and Down)

Skill Level: Beginner

Navigate command history without retyping.

Up Arrow  โ†’ Previous command
Down Arrow โ†’ Next command
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Quickly re-execute previous commands.


16. Autocomplete File and Command Names (Tab)

Skill Level: Beginner

Type part of a filename or command, then press Tab to autocomplete.

ls /var/lo<Tab>  # Expands to /var/log
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Reduces typing mistakes.


17. Ignore Duplicate Commands in History (HISTCONTROL)

Skill Level: Advanced

Add this to your .bashrc to prevent duplicate history entries:

export HISTCONTROL=ignoredups
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Keeps history cleaner.


18. Repeat the First Argument of the Last Command (!^)

Skill Level: Advanced

cp myfile.txt backup/
mv !^ newlocation/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Real-World Use Case: Saves keystrokes and reduces mistakes.


Final Thoughts: Save Hours with Bash Shortcuts

Mastering these shortcuts will speed up your workflow, reduce errors, and make you more efficient in the terminal.


Want a Structured Bash Reference?

If you need a Beginner Friendly Bash guide with easy to follow tips and explanations, check out my Bash Cheat Sheet:

๐Ÿ‘‰ Download the Bash Cheat Sheet for just $3.99

Discussion: Whatโ€™s Your Favorite Bash Shortcut?

Drop a comment below and share your most-used Bash keyboard shortcut!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair โ€ข

Your example 2, the "edit(?) and reuse..." one, has a typo:

cp file1.txt /backup/
rm !$
Enter fullscreen mode Exit fullscreen mode

will execute rm /backup/, not rm file1.txt.

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

๐Ÿ‘‹ Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay