← Back to Blog

How to add a screen text capture (OCR) shortcut on Linux (Wayland)

·
linux wayland cosmic productivity shell

Why set this up?

  • Select any region of your screen and get the text instantly on your clipboard

  • No extra apps to open — just a keyboard shortcut

  • Built entirely from small, composable CLI tools already common on Wayland

What you’ll need

Install these four tools:

  • grim — takes screenshots on Wayland

  • slurp — lets you select a screen region with your mouse

  • tesseract — runs OCR (text recognition) on an image

  • wl-copy (part of wl-clipboard) — copies text to the clipboard

# Arch
sudo pacman -S grim slurp tesseract tesseract-data-eng wl-clipboard
# Fedora
sudo dnf install grim slurp tesseract wl-clipboard
# Debian/Ubuntu
sudo apt install grim slurp tesseract-ocr wl-clipboard

The basic command

Keyboard shortcut daemons usually run commands directly, without going through a shell — so pipes (|) and command substitution ($()) need to be wrapped in sh -c to work at all. This bare version, with no notification and no backgrounding, is the minimum needed to get the pipeline running from a shortcut.

# This selects a region, OCRs it, and copies the text.
# No feedback, and can feel like it "hangs" briefly.
sh -c 'grim -g "$(slurp)" - | tesseract stdin stdout | wl-copy'

Note: pasting the un-wrapped pipeline (without sh -c) into a shortcut field will not work — it only runs correctly in an interactive terminal.


The working command

Running the pipeline in the background (&) keeps the shortcut from feeling stuck while tesseract processes the image, and adding notify-send gives you feedback that it actually ran.

# Backgrounded + notification on completion.
sh -c 'grim -g "$(slurp)" - | tesseract stdin stdout | wl-copy && notify-send "OCR copied" &'

Adding it as a shortcut

In your desktop environment’s keyboard shortcut settings, add a new custom shortcut:

  • Command: sh -c 'grim -g "$(slurp)" - | tesseract stdin stdout | wl-copy && notify-send "OCR copied" &'

  • Shortcut: pick any unused key combination, e.g. Ctrl+Alt+S

Press it, select a region containing text, and you should see an “OCR copied” notification with the text on your clipboard.


Improvement: fix false-positive notifications

There’s one rough edge left. The command above says “OCR copied” even when nothing useful actually happened — if you cancel the selection (hit Escape in slurp), or select an area with no readable text, it still reports success. That’s because && only checks that each command in the pipe exited without an error — not whether any real text was produced. wl-copy happily “succeeds” copying an empty string.

The fix is to capture the OCR result into a variable, strip blank output, and only report success when there’s actual text:

sh -c 'text=$(grim -g "$(slurp)") 2>/dev/null
[ -z "$text" ] && exit
text=$(printf "%s" "$text" | tesseract stdin stdout 2>/dev/null)
text=$(printf "%s" "$text" | sed "/^[[:space:]]*$/d")
if [ -n "$text" ]; then
    printf "%s" "$text" | wl-copy
    notify-send "OCR copied" "$(printf %.100s "$text")"
else
    notify-send "OCR failed" "No text detected"
fi &'

Now:

  • Canceling the selection exits quietly, no notification at all.

  • A selection with no readable text shows “OCR failed” instead of a false “OCR copied.”

  • A successful capture shows a preview of the copied text, so you can catch OCR mistakes at a glance.


Final command

Paste this as your shortcut command for the most reliable version:

sh -c 'text=$(grim -g "$(slurp)") 2>/dev/null; [ -z "$text" ] && exit; text=$(printf "%s" "$text" | tesseract stdin stdout 2>/dev/null); text=$(printf "%s" "$text" | sed "/^[[:space:]]*$/d"); if [ -n "$text" ]; then printf "%s" "$text" | wl-copy; notify-send "OCR copied" "$(printf %.100s "$text")"; else notify-send "OCR failed" "No text detected"; fi &'

Note: restart your keyboard shortcut daemon (or log out/in) if a newly saved shortcut doesn’t seem to fire.

Bind it to any free key combination, and you’ve got a reliable screenshot-to-text shortcut that only tells you “copied” when it actually copied something.


A note on built-in tooling

It’s worth pointing out that this whole workaround exists because newer desktop environments like COSMIC don’t yet ship a screenshot tool with OCR built in. KDE’s default screenshot utility, Spectacle, has had this kind of “extract text from screenshot” capability built directly into its UI for a while now — no piping together four separate CLI tools required.

As younger DEs like COSMIC mature, it’d be a genuine usability win if their default screenshot tools followed KDE’s lead here — baking OCR-to-clipboard in as a first-class option rather than leaving users to assemble it themselves from grim, slurp, tesseract, and wl-copy. Until then, though, the shortcut above gets you the same result.

👏 Nice work!

Don’t forget to follow 🌏 my socials and subscribe to my 💌 newsletter.