DEV Community

KB Acuff
KB Acuff

Posted on

Clipboard Shortcut for AI

📋 Stop Copying Terminal Output Manually! Meet ca, a Clipboard Shortcut for Termux

Tags: #android #termux #bash #opensource

If you use Termux on Android to write code, run scripts, manage Git repositories, or build applications, you've probably encountered this annoying workflow:

  1. Run a command.
  2. Watch the terminal fill with output.
  3. Try to select the text you need using your finger.
  4. Accidentally select the wrong lines.
  5. Start over.
  6. Question your life choices.

There's a better way. 😅

Meet ca, a small Bash utility that runs a command normally while automatically copying its combined output to your Android clipboard.

Instead of manually selecting terminal output, you can run:

ca git status
Enter fullscreen mode Exit fullscreen mode

Or:

ca npm test
Enter fullscreen mode Exit fullscreen mode

Or even:

ca bash "$HOME/my-build-script.sh"
Enter fullscreen mode Exit fullscreen mode

The command executes normally, displays its output in Termux, captures the output, and copies it to your clipboard.

You can then paste the results directly into ChatGPT, Gemini, a GitHub issue, a text editor, or another application.

It's a simple tool, but when you're developing applications entirely on an Android phone, eliminating repetitive clipboard work makes a real difference.

Let's build it! 🚀


🧠 What Is ca?

ca is a shortcut for a Bash script called copyall.

It combines three useful operations:

  • Execute: Run the command you specify.
  • Capture: Collect its standard output and standard error.
  • Copy: Send the captured output to the Android clipboard.

Here's the basic workflow:

  1. You run a command using ca.
  2. The command executes.
  3. Its output appears in Termux.
  4. The output is captured in a temporary file.
  5. The captured text is copied to the Android clipboard.
  6. You paste it wherever you need it.

The utility uses standard Bash tools, including tee, mktemp, and trap, along with Termux's clipboard integration.

You don't need Python, Node.js, a special IDE, or a complicated development environment.

You just need Termux, Bash, and Termux:API.


📱 Requirements

Before installing ca, make sure you have the following:

Requirement Purpose
Android Runs the Termux environment
Termux Provides the Linux-like terminal
Bash Executes the scripts
Termux:API package Provides the clipboard command
Termux:API Android companion app Connects Termux to Android's clipboard

Install Termux:API

Open Termux and run:

pkg update
pkg install termux-api
Enter fullscreen mode Exit fullscreen mode

You must also install the Termux:API companion app from a compatible distribution source.

Important: The companion app and Termux should come from compatible sources. Mixing incompatible APK signatures can cause installation or integration problems.

Official resources:


⚡ Installation: Get ca Running in Termux

We'll create two files:

~/bin/
├── ca
└── copyall
Enter fullscreen mode Exit fullscreen mode

The ca executable is a lightweight wrapper that forwards your command to copyall.

The copyall script handles execution, output capture, and clipboard copying.

Step 1: Create the installation script

Copy the complete block below into Termux and run it.

The installer creates both commands, backs up existing versions, configures your PATH, and checks the scripts for syntax errors.

cat > "$HOME/install-ca.sh" <<'INSTALLER'
#!/data/data/com.termux/files/usr/bin/bash

set -u

echo "=========================================="
echo " CA / COPYALL INSTALLER"
echo "=========================================="

BIN="$HOME/bin"
CA="$BIN/ca"
COPYALL="$BIN/copyall"

mkdir -p "$BIN" || {
    echo "[FAIL] Cannot create $BIN"
    exit 1
}

# Back up existing scripts before replacing them.
for FILE in "$CA" "$COPYALL"; do
    if [ -f "$FILE" ]; then
        BACKUP="${FILE}.backup.$(date +%Y%m%d-%H%M%S)"

        cp -p "$FILE" "$BACKUP" || {
            echo "[FAIL] Could not back up $FILE"
            exit 1
        }

        echo "[BACKUP] $BACKUP"
    fi
done

echo
echo "=== INSTALLING CA ==="

cat > "$CA" <<'CA_SCRIPT'
#!/data/data/com.termux/files/usr/bin/bash

exec "$HOME/bin/copyall" "$@"
CA_SCRIPT

chmod +x "$CA" || {
    echo "[FAIL] Could not make ca executable"
    exit 1
}

echo "[PASS] $CA"

echo
echo "=== INSTALLING COPYALL ==="

cat > "$COPYALL" <<'COPYALL_SCRIPT'
#!/data/data/com.termux/files/usr/bin/bash

# copyall
# Execute a command, capture combined output,
# display it live, and copy it to Android's clipboard.

if [ "$#" -eq 0 ]; then
    echo
    echo "COPYALL - Copy terminal output to Android clipboard"
    echo
    echo "Usage:"
    echo "  ca <command> [arguments...]"
    echo
    echo "Examples:"
    echo "  ca git status"
    echo "  ca git diff"
    echo "  ca npm test"
    echo "  ca bash \"\$HOME/my-script.sh\""
    echo
    exit 0
fi

TMP_FILE="$(mktemp "${TMPDIR:-$PREFIX/tmp}/copyall.XXXXXX")" || {
    echo "[FAIL] Could not create temporary file"
    exit 1
}

cleanup() {
    rm -f "$TMP_FILE"
}

trap cleanup EXIT

echo
echo "------------------------------------------"
echo " COPYALL"
echo " Command: $*"
echo "------------------------------------------"
echo

# Capture stdout and stderr while displaying output live.
"$@" 2>&1 | tee "$TMP_FILE"

# Preserve the executed command's exit status.
COMMAND_STATUS=${PIPESTATUS[0]}

echo
echo "------------------------------------------"

if command -v termux-clipboard-set >/dev/null 2>&1 &&
   termux-clipboard-set < "$TMP_FILE"; then

    BYTES="$(wc -c < "$TMP_FILE" | tr -d ' ')"
    LINES="$(wc -l < "$TMP_FILE" | tr -d ' ')"

    echo "COPIED: $LINES lines / $BYTES bytes"
    echo "Clipboard: Android clipboard updated"
else
    echo "[FAIL] Could not update Android clipboard."
    echo "Check the Termux:API package and companion app."

    COMMAND_STATUS=1
fi

echo "Command exit status: $COMMAND_STATUS"
echo "------------------------------------------"
echo

exit "$COMMAND_STATUS"
COPYALL_SCRIPT

chmod +x "$COPYALL" || {
    echo "[FAIL] Could not make copyall executable"
    exit 1
}

echo "[PASS] $COPYALL"

echo
echo "=== CONFIGURING PATH ==="

touch "$HOME/.bashrc"

if ! grep -qxF 'export PATH="$HOME/bin:$PATH"' "$HOME/.bashrc"; then
    printf '\n%s\n' 'export PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc"
    echo "[PASS] Added ~/bin to PATH"
else
    echo "[PASS] ~/bin already configured"
fi

export PATH="$HOME/bin:$PATH"

echo
echo "=== CHECKING CLIPBOARD SUPPORT ==="

if command -v termux-clipboard-set >/dev/null 2>&1; then
    echo "[PASS] termux-clipboard-set is available"
else
    echo "[WARN] termux-clipboard-set is missing"
    echo "Install it with: pkg install termux-api"
fi

echo
echo "=== SYNTAX CHECKS ==="

if bash -n "$CA" && bash -n "$COPYALL"; then
    echo "[PASS] Both scripts have valid Bash syntax"
else
    echo "[FAIL] Syntax check failed"
    exit 1
fi

echo
echo "=========================================="
echo " INSTALLATION COMPLETE"
echo "=========================================="
echo
echo "Activate your PATH with:"
echo '  source "$HOME/.bashrc"'
echo
echo "Test the command with:"
echo "  ca printf 'CA WORKS\\n'"
INSTALLER

chmod +x "$HOME/install-ca.sh"

bash -n "$HOME/install-ca.sh" &&
bash "$HOME/install-ca.sh"
Enter fullscreen mode Exit fullscreen mode

Step 2: Activate the command

Run:

source "$HOME/.bashrc"
Enter fullscreen mode Exit fullscreen mode

Check that both commands are available:

command -v ca
command -v copyall
Enter fullscreen mode Exit fullscreen mode

You should see paths similar to:

/data/data/com.termux/files/home/bin/ca
/data/data/com.termux/files/home/bin/copyall
Enter fullscreen mode Exit fullscreen mode

Step 3: Test the clipboard

Run:

ca printf 'CA IS WORKING!\n'
Enter fullscreen mode Exit fullscreen mode

You should see the output in Termux and a message confirming that the clipboard was updated.

Now paste your clipboard contents into a text field.

You should see:

CA IS WORKING!
Enter fullscreen mode Exit fullscreen mode

Congratulations! Your clipboard workflow just became considerably less irritating. 🎉


🛠️ How to Use ca

Once installed, you can prefix most ordinary terminal commands with ca.

1. Copy Git status

Instead of:

git status
Enter fullscreen mode Exit fullscreen mode

Use:

ca git status
Enter fullscreen mode Exit fullscreen mode

The output appears in Termux and is copied to your clipboard.

This is particularly useful when asking an AI assistant to diagnose Git problems.

2. Copy Git diffs

ca git diff
Enter fullscreen mode Exit fullscreen mode

You can then paste the changes into an AI coding assistant for review.

For staged changes:

ca git diff --cached
Enter fullscreen mode Exit fullscreen mode

3. Copy test results

For Node.js projects:

ca npm test
Enter fullscreen mode Exit fullscreen mode

For Python projects:

ca python -m pytest
Enter fullscreen mode Exit fullscreen mode

These examples assume the relevant testing tools and project scripts are installed.

4. Copy build output

ca npm run build
Enter fullscreen mode Exit fullscreen mode

If a build fails, the output is already on your clipboard.

You can paste the errors into ChatGPT or Gemini without manually selecting dozens of lines.

5. Copy a script's output

Suppose you have a build script:

~/build-project.sh
Enter fullscreen mode Exit fullscreen mode

Run:

ca bash "$HOME/build-project.sh"
Enter fullscreen mode Exit fullscreen mode

Its output is captured and copied automatically.

6. Inspect a configuration file

ca cat "$HOME/.bashrc"
Enter fullscreen mode Exit fullscreen mode

Be careful with sensitive files. Clipboard contents can be pasted into another application, and some Android environments retain clipboard history.

Never casually copy API keys, passwords, private tokens, or other secrets into an AI conversation.

7. Capture errors from commands

ca git status
Enter fullscreen mode Exit fullscreen mode

If Git returns an error, its error message is included in the captured output.

The same principle applies to many other commands.

This is especially useful when troubleshooting unfamiliar software on Android.


🔬 How Does copyall Work?

The implementation is small, but several details make it useful.

1. It creates a temporary output file

TMP_FILE="$(mktemp "${TMPDIR:-$PREFIX/tmp}/copyall.XXXXXX")"
Enter fullscreen mode Exit fullscreen mode

This creates a uniquely named temporary file.

The output is written there while the command runs.

2. It captures both output streams

"$@" 2>&1 | tee "$TMP_FILE"
Enter fullscreen mode Exit fullscreen mode

Here's what that means:

  • "$@" executes the command and its arguments.
  • 2>&1 redirects standard error to standard output.
  • tee displays the output while writing a copy to the temporary file.

This means you can watch the command execute normally.

3. It preserves the command's exit status

COMMAND_STATUS=${PIPESTATUS[0]}
Enter fullscreen mode Exit fullscreen mode

Bash's PIPESTATUS array records the exit status of commands in the most recent pipeline.

The script saves the first entry immediately because subsequent commands can overwrite that array.

This allows the script to report whether the original command succeeded, even though the output was piped through tee.

4. It copies the captured output

termux-clipboard-set < "$TMP_FILE"
Enter fullscreen mode Exit fullscreen mode

Termux:API transfers the contents of the temporary file to Android's clipboard.

You can then paste the output into another application.

5. It cleans up

trap cleanup EXIT
Enter fullscreen mode Exit fullscreen mode

The cleanup function removes the temporary file when the script exits normally.

This prevents the script from accumulating output files every time you use it.


⚠️ Important Limitations

ca is useful, but it's not magic.

Clipboard size

Very large outputs may exceed practical clipboard or application limits.

For enormous build logs, consider saving the output to a permanent file instead.

Interactive commands

Commands that require interactive input may behave differently when their output is piped through tee.

Examples include some terminal editors, interactive shells, and full-screen terminal applications.

For these, use the original command without ca.

Binary output

ca is intended for text output.

Do not use it to copy binary files or other non-text data into your clipboard.

Background processes

Commands that launch background processes and exit immediately may continue producing output after ca has finished capturing it.

The script is best suited to commands that complete normally in the foreground.

Exit statuses

The script returns the original command's exit status when clipboard copying succeeds.

If clipboard copying fails, the script returns a failure status.

This is useful when running automated scripts that depend on successful command execution.


🐛 Troubleshooting

Problem: ca: command not found

Check whether the executable exists:

ls -l "$HOME/bin/ca"
Enter fullscreen mode Exit fullscreen mode

Then check your PATH:

echo "$PATH"
Enter fullscreen mode Exit fullscreen mode

If necessary, add the directory:

grep -qxF 'export PATH="$HOME/bin:$PATH"' "$HOME/.bashrc" ||
echo 'export PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc"

source "$HOME/.bashrc"
Enter fullscreen mode Exit fullscreen mode

Problem: termux-clipboard-set: command not found

Install the package:

pkg update
pkg install termux-api
Enter fullscreen mode Exit fullscreen mode

Also verify that the compatible Termux:API companion app is installed.

Problem: The clipboard doesn't update

Test the clipboard command directly:

printf 'Clipboard test\n' | termux-clipboard-set
Enter fullscreen mode Exit fullscreen mode

Then paste the clipboard contents into another application.

If it fails, check the Termux:API installation, Android permissions, and compatibility between the installed applications.

Problem: The script reports an error even though the command worked

Check whether the clipboard operation failed.

The script treats clipboard failure as an overall failure because one of its main purposes is to copy the output.

That behavior is intentional.


💡 Why I Built This

Working on software from an Android phone presents some unique challenges.

You may be using Termux to:

  • Develop web applications.
  • Manage GitHub repositories.
  • Run JavaScript and Python scripts.
  • Build websites.
  • Test APIs.
  • Troubleshoot dependency problems.
  • Work with AI coding assistants.

In these workflows, sharing terminal output is a routine task.

You run a command, inspect the result, and often need to share that result with another tool or person.

Doing that manually over and over becomes tedious.

ca removes that repetitive step.

It is not a replacement for Termux or Bash. It is a small productivity utility designed to make them more convenient to use.

And because it's written in Bash, the implementation is easy to inspect and modify.


🚀 Ideas for Future Improvements

There are several ways this utility could evolve.

1. Add output size warnings

Warn the user when the captured output is particularly large.

2. Add optional log saving

Allow users to save a permanent copy of command output.

For example:

ca --save build.log npm run build
Enter fullscreen mode Exit fullscreen mode

That feature would require extending the argument handling in the current script.

3. Add a clipboard-only mode

Allow users to capture output without displaying it in the terminal.

4. Add redaction

Automatically detect common secret formats before copying output.

This should be treated as an additional safeguard, not a guarantee that all secrets will be detected.

5. Add AI debugging integration

A future version could send selected command output to a configured AI API for troubleshooting.

That would require additional code, API credentials, and careful handling of sensitive data.

For now, the simple clipboard workflow already solves a common problem.


📦 Quick Reference

Once installed, these are the commands you'll use most often:

Task Command
Check Git status ca git status
Copy Git changes ca git diff
Run Node.js tests ca npm test
Build a project ca npm run build
Run a shell script ca bash "$HOME/script.sh"
Display a file ca cat "$HOME/file.txt"
Check Node.js ca node --version
Check Python ca python --version

The pattern is simple:

ca <command> [arguments...]
Enter fullscreen mode Exit fullscreen mode

Run the command, let Termux display the output, and paste the captured results wherever you need them.


Final Thoughts

Sometimes the most useful development tools aren't complicated frameworks or enormous applications.

Sometimes they're tiny utilities that eliminate an annoying task you perform dozens of times a day.

ca is one of those tools.

If you use Termux on Android and regularly share terminal output with AI assistants, developers, or support forums, give it a try.

Install it, test it, and spend less time wrestling with text selection on a touchscreen. 📱

If you improve the script or build something useful on top of it, share your ideas with the community.

Happy coding! 💻


Suggested Dev.to title:

ca: A Tiny Termux Command That Automatically Copies Terminal Output to Your Android Clipboard

Suggested Dev.to tags:

#termux #android #bash #productivity

Top comments (0)