|
| 1 | +--- |
| 2 | +layout: base.njk |
| 3 | +title: "Keyboard Shortcuts (Readline)" |
| 4 | +excerpt: "The hidden shortcuts that make terminal pros look like wizards." |
| 5 | +tags: posts |
| 6 | +--- |
| 7 | + |
| 8 | +# Keyboard Shortcuts (Readline) ⌨️ |
| 9 | + |
| 10 | +Have you ever watched someone blast through the terminal, editing commands at lightning speed without reaching for the mouse? They aren't typing faster — they're using **Readline shortcuts**. |
| 11 | + |
| 12 | +## What is Readline? |
| 13 | + |
| 14 | +**[GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html)** is a tiny, invisible library built into **Bash** (and many other terminal programs) that handles everything you type at the command line. It's the reason you can press `Up Arrow` to recall your last command or hit `Tab` to auto-complete a file name. |
| 15 | + |
| 16 | +But `Up Arrow` and `Tab` are just the tip of the iceberg. Readline has dozens of keyboard shortcuts that most people never discover — and they work **everywhere**, not just in your shell. They work in `python`, `node`, `mysql`, and almost any interactive command-line tool. |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> **Fish Shell Users**: Fish doesn't use GNU Readline internally — it has its own editor. However, Fish supports most of the same shortcuts listed here by default, so these habits will serve you well in either shell! |
| 20 | +
|
| 21 | +--- |
| 22 | + |
| 23 | +## ✂️ The Essential Shortcuts |
| 24 | + |
| 25 | +These are the shortcuts you'll use every single day. They all work without a mouse and are dramatically faster than using the arrow keys. |
| 26 | + |
| 27 | +### Moving the Cursor |
| 28 | + |
| 29 | +| Shortcut | What it does | |
| 30 | +| :--- | :--- | |
| 31 | +| **`Ctrl+A`** | Jump to the **beginning** of the line | |
| 32 | +| **`Ctrl+E`** | Jump to the **end** of the line | |
| 33 | +| **`Alt+F`** | Jump forward one **word** | |
| 34 | +| **`Alt+B`** | Jump backward one **word** | |
| 35 | + |
| 36 | +> [!TIP] |
| 37 | +> **Think of it this way:** `A` = stArt, `E` = End, `F` = Forward, `B` = Backward. |
| 38 | +
|
| 39 | +### Deleting Text |
| 40 | + |
| 41 | +| Shortcut | What it does | |
| 42 | +| :--- | :--- | |
| 43 | +| **`Ctrl+W`** | Delete the **word** before the cursor | |
| 44 | +| **`Ctrl+K`** | Delete everything **after** the cursor (to end of line) | |
| 45 | +| **`Ctrl+U`** | Delete everything **before** the cursor (to start of line) | |
| 46 | +| **`Alt+D`** | Delete the word **after** the cursor | |
| 47 | + |
| 48 | +### The "Undo" and "Paste" |
| 49 | + |
| 50 | +Here's the secret most people don't know: when you delete text with `Ctrl+W`, `Ctrl+K`, or `Ctrl+U`, the deleted text isn't gone — it's saved in a clipboard called the **kill ring**. |
| 51 | + |
| 52 | +| Shortcut | What it does | |
| 53 | +| :--- | :--- | |
| 54 | +| **`Ctrl+Y`** | **Paste** (\"yank\") the last deleted text back | |
| 55 | +| **`Ctrl+_`** | **Undo** the last edit | |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 🎯 Real-World Examples |
| 60 | + |
| 61 | +### Fixing a Typo at the Start of a Long Command |
| 62 | +You just typed a monster command and realized you forgot `sudo`: |
| 63 | +``` |
| 64 | +systemctl enable --now sshd |
| 65 | +``` |
| 66 | +Instead of pressing `Home`, then typing `sudo `: |
| 67 | +- **`Ctrl+A`** → jump to the start |
| 68 | +- Type `sudo ` → done! |
| 69 | + |
| 70 | +### Editing Your Last Command Instead of Retyping It |
| 71 | +You just copied a file to the wrong place: |
| 72 | +```bash |
| 73 | +cp ~/Downloads/wallpaper.png ~/Pictures/screenshots/ |
| 74 | +``` |
| 75 | +You actually meant to put it in `~/Pictures/wallpapers/`. Instead of retyping the whole thing: |
| 76 | +- Press **`Up Arrow`** to recall the command |
| 77 | +- Press **`Ctrl+W`** to delete the last word (`~/Pictures/screenshots/`) |
| 78 | +- Type the correct destination: `~/Pictures/wallpapers/` |
| 79 | + |
| 80 | +### Grabbing the Last Argument with `Alt+.` |
| 81 | +**`Alt+.`** pastes the **last argument** of the previous command. This is a huge time-saver whenever you're working with the same file or directory across multiple commands. |
| 82 | + |
| 83 | +You just checked a config file: |
| 84 | +```bash |
| 85 | +cat /etc/NetworkManager/conf.d/wifi_backend.conf |
| 86 | +``` |
| 87 | +Now you want to edit it. Type `nano`, then hit **`Alt+.`**: |
| 88 | +```bash |
| 89 | +nano /etc/NetworkManager/conf.d/wifi_backend.conf |
| 90 | +``` |
| 91 | +No retyping that long path! This works just as well for directories — after running `mkdir -p ~/Games/emulation/roms/gba`, you can type `cd` and hit **`Alt+.`** to jump straight into the new directory. |
| 92 | + |
| 93 | +### Wiping a Command You Changed Your Mind About |
| 94 | +You're halfway through a command and decide you don't want to run it: |
| 95 | +- **`Ctrl+C`** cancels and gives you a fresh prompt |
| 96 | +- Or **`Ctrl+U`** clears the line but keeps it in the kill ring in case you want it back with **`Ctrl+Y`** |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## 🧠 The Cheat Sheet |
| 101 | + |
| 102 | +Here's everything in one place for quick reference: |
| 103 | + |
| 104 | +| Category | Shortcut | Action | |
| 105 | +| :--- | :--- | :--- | |
| 106 | +| **Move** | `Ctrl+A` / `Ctrl+E` | Start / End of line | |
| 107 | +| **Move** | `Alt+F` / `Alt+B` | Forward / Back one word | |
| 108 | +| **Delete** | `Ctrl+W` | Delete word before cursor | |
| 109 | +| **Delete** | `Ctrl+U` / `Ctrl+K` | Delete to start / end of line | |
| 110 | +| **Delete** | `Alt+D` | Delete word after cursor | |
| 111 | +| **Paste** | `Ctrl+Y` | Yank (paste) last deleted text | |
| 112 | +| **Undo** | `Ctrl+_` | Undo last edit | |
| 113 | +| **History** | `Up` / `Down` | Previous / next command | |
| 114 | +| **History** | `Ctrl+R` | Reverse search through history | |
| 115 | +| **Magic** | `Alt+.` | Insert last argument of previous command | |
| 116 | +| **Cancel** | `Ctrl+C` | Abort current command | |
| 117 | +| **Clear** | `Ctrl+L` | Clear the screen (same as `clear`) | |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +These shortcuts take about a day to become muscle memory, and once they click, you'll wonder how you ever lived without them. The terminal stops feeling like a text box and starts feeling like an instrument. |
| 122 | + |
| 123 | +{% next_chapter %} |
0 commit comments