Dev Tip: Automatically Copy Console Output to Clipboard¶
Category: Linux / Shell Productivity
Platform: Linux (zsh + Konsole/KDE)
The Problem¶
When working with AI assistants, you often need to copy terminal output and paste it into the chat. This usually means:
Run command
Select output with mouse
Copy
Switch to browser
Paste
That’s too many steps.
The Solution: Auto-capture via preexec / precmd¶
Add this to your ~/.zshrc:
# === AUTO-OUTPUT LOGGER ===
# Automatically saves console output to ~/t.txt and copies to clipboard.
# Toggle: set AUTO_CLIPBOARD=true/false
AUTO_CLIPBOARD=true
# Redirect stdout+stderr to ~/t.txt before each command
preexec() {
case "$1" in
sudo*|su*) return ;;
*) exec > >(tee ~/t.txt) 2>&1 ;;
esac
}
precmd() {
exec >/dev/tty 2>&1
if [ "$AUTO_CLIPBOARD" = "true" ] && [ -s ~/t.txt ]; then
cleaned=$(cat ~/t.txt \
| sed 's/\][0-9]*;[^]]*\][0-9]*;//g; s/^[0-9]*;//g' \
| sed "s|$HOME|~|g" \
| sed 's/[^[:print:]]//g' \
| grep -v '^$')
if [ -n "$cleaned" ]; then
echo "$cleaned" | xclip -selection clipboard
echo "[📋 In Zwischenablage kopiert]"
fi
fi
}
Then reload:
source ~/.zshrc
Result¶
After every command, the output is automatically in your clipboard — ready to paste into your AI chat with Ctrl+V.
The output is also always saved in ~/t.txt for reference.
How it works¶
Part |
What it does |
|---|---|
|
Runs before each command, redirects output to |
|
Runs after each command, restores stdout and copies to clipboard |
|
Saves output to file while still showing it in terminal |
|
Strips KDE Konsole title escape sequences ( |
|
Copies cleaned output to clipboard |
Requirements¶
# Install xclip if not present
sudo pacman -S xclip # Manjaro/Arch
sudo apt install xclip # Ubuntu/Debian
⚠️ What NOT to do¶
Do not use fc -ln -1 | bash to re-execute the last command:
# ❌ DANGEROUS - do not use!
precmd() {
output=$(fc -ln -1 | bash 2>&1) # Re-executes last command!
echo "$output" | xclip -selection clipboard
}
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.
The preexec/precmd approach above captures output during execution — safe and reliable.