|
| 1 | +# Dev Tip: Automatically Copy Console Output to Clipboard |
| 2 | + |
| 3 | +**Category:** Linux / Shell Productivity |
| 4 | +**Platform:** Linux (zsh + Konsole/KDE) |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## The Problem |
| 9 | + |
| 10 | +When working with AI assistants, you often need to copy terminal output and paste it into the chat. This usually means: |
| 11 | +1. Run command |
| 12 | +2. Select output with mouse |
| 13 | +3. Copy |
| 14 | +4. Switch to browser |
| 15 | +5. Paste |
| 16 | + |
| 17 | +That's too many steps. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## The Solution: Auto-capture via `preexec` / `precmd` |
| 22 | + |
| 23 | +Add this to your `~/.zshrc`: |
| 24 | + |
| 25 | +```bash |
| 26 | +# === AUTO-OUTPUT LOGGER === |
| 27 | +# Automatically saves console output to ~/t.txt and copies to clipboard. |
| 28 | +# Toggle: set AUTO_CLIPBOARD=true/false |
| 29 | +AUTO_CLIPBOARD=true |
| 30 | + |
| 31 | +preexec() { |
| 32 | + # Redirect stdout+stderr to ~/t.txt before each command |
| 33 | + exec > >(tee ~/t.txt) 2>&1 |
| 34 | +} |
| 35 | + |
| 36 | +precmd() { |
| 37 | + # Restore stdout after command finishes |
| 38 | + exec >/dev/tty 2>&1 |
| 39 | + if [ "$AUTO_CLIPBOARD" = "true" ] && [ -s ~/t.txt ]; then |
| 40 | + # Filter out ANSI escape codes and KDE Konsole title sequences |
| 41 | + cat ~/t.txt | sed 's/\][0-9]*;[^]]*\][0-9]*;//g; s/^[0-9]*;//g' \ |
| 42 | + | xclip -selection clipboard |
| 43 | + echo "[📋 In Zwischenablage kopiert]" |
| 44 | + fi |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Then reload: |
| 49 | +```bash |
| 50 | +source ~/.zshrc |
| 51 | +``` |
| 52 | + |
| 53 | +### Result |
| 54 | + |
| 55 | +After every command, the output is automatically in your clipboard — ready to paste into your AI chat with **Ctrl+V**. |
| 56 | + |
| 57 | +The output is also always saved in `~/t.txt` for reference. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## How it works |
| 62 | + |
| 63 | +| Part | What it does | |
| 64 | +|------|-------------| |
| 65 | +| `preexec()` | Runs before each command, redirects output to `~/t.txt` | |
| 66 | +| `precmd()` | Runs after each command, restores stdout and copies to clipboard | |
| 67 | +| `tee ~/t.txt` | Saves output to file while still showing it in terminal | |
| 68 | +| `sed '...'` | Strips KDE Konsole title escape sequences (`]2;...` `]1;`) | |
| 69 | +| `xclip` | Copies cleaned output to clipboard | |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## Requirements |
| 74 | + |
| 75 | +```bash |
| 76 | +# Install xclip if not present |
| 77 | +sudo pacman -S xclip # Manjaro/Arch |
| 78 | +sudo apt install xclip # Ubuntu/Debian |
| 79 | +``` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## ⚠️ What NOT to do |
| 84 | + |
| 85 | +Do **not** use `fc -ln -1 | bash` to re-execute the last command: |
| 86 | + |
| 87 | +```bash |
| 88 | +# ❌ DANGEROUS - do not use! |
| 89 | +precmd() { |
| 90 | + output=$(fc -ln -1 | bash 2>&1) # Re-executes last command! |
| 91 | + echo "$output" | xclip -selection clipboard |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +This re-executes every command after it finishes, which can cause destructive side effects — for example overwriting files, re-running `git commit`, re-running `sed -i`, etc. |
| 96 | + |
| 97 | +The `preexec`/`precmd` approach above captures output **during** execution — safe and reliable. |
0 commit comments