Skip to content

Copy Code from a Screenshot on Mac Without Retyping It

By Eduard Bruch, developer of Optic · Updated

To copy code from a screenshot on a Mac, open the image, select the code with an OCR tool, and paste the text into your editor. Then read it once against the image, because OCR can mix up characters like l and 1 or O and 0.

Short answer: for images in Preview or Photos, try Live Text first. For code in any app window, video or chat, my pick is Optic, a menu bar app that copies the text in any area you drag across the screen ($4.99, a one-time purchase on the Mac App Store).

Disclosure: I build Optic.

Why retyping code goes wrong

Code is strict. One wrong quote, a missing bracket or a confused l and 1 breaks a command, and the error often does not point at the real cause. Retyping a long docker run line or a curl call with several flags is exactly where these slips happen.

Code ends up in images all the time: snippets posted as pretty screenshots on social media, tutorials that show examples as pictures, old wiki pages full of terminal captures, and code forwarded as screenshots in Slack or Teams.

Try Live Text first

macOS Live Text works where Apple supports it: images in Preview, Photos, Quick Look and Safari. Open the screenshot in Preview, move the pointer over the code until it turns into a text cursor, select, and press Command-C.

Live Text does not work on arbitrary app windows, video calls or text drawn inside an app's interface. If the code is in a video player, a remote desktop or a screen share, you need a screen OCR tool.

Copy the code with Optic

Optic reads whatever is on screen inside the box you draw. It does not open image files itself, so open the screenshot in Preview, a browser or any viewer first.

  1. Click the Optic icon in the menu bar.
  2. Choose Capture Text.
  3. Drag a box over the code. Leave out line numbers if the image has them.
  4. Paste into your editor with Command-V.

A small "Copied to clipboard" confirmation appears when the text is ready.

Turn on Code mode

In Optic's Settings, Code mode is meant for this job. It keeps the indentation of each line, based on where the text sits in the image, and it turns off language correction so the recognizer does not "fix" identifiers into dictionary words. Leave it on while you copy code and switch it off for normal prose.

Line breaks are kept by default (Preserve), which is what you want for code. The other options, Remove and Smart merge, are for paragraphs.

Check before you run it

Recognition runs on your Mac with Apple's Vision text recognition. It is good on clear, high-contrast text, but it is still OCR. Before you paste a command into a terminal, compare it with the image, especially:

  • quotes and backticks
  • l, 1, I and O, 0
  • hyphens and long dashes in flags
  • indentation in Python or YAML

Bigger text recognizes better, so zoom the image in before you drag if the font is small.

Several snippets in a row

Every capture lands in Optic's history in the menu bar popover, which keeps the last 20 captures with a thumbnail. If a tutorial shows five steps, capture them one after another and copy each again from the history when you need it. The history stays on your Mac.

Where Optic does not fit

  • It does not open image or PDF files. The code has to be visible on screen.
  • It works on one selection at a time. It does not process folders of screenshots or whole documents.
  • It copies plain text. It does not keep syntax colors or detect the programming language.
  • It needs the macOS Screen Recording permission to read the screen.

For more on this topic, see OCR for code snippets on Mac and how to copy text from anything on your screen. If you are comparing apps, there is a roundup of screen OCR apps for Mac.

FAQ

Can a Mac copy code from an image without extra apps?

Often, yes. Live Text in Preview, Photos, Quick Look and Safari lets you select text in images. It does not work on video, screen shares or text inside other apps' windows. For code anywhere else on screen, my pick is Optic with Code mode on.

Does Optic keep the indentation of code?

With Code mode on in Settings, Optic keeps the indentation of each line based on its position in the image. Check the result for Python and YAML, where indentation matters.

Does Optic need an internet connection?

No. Text recognition runs on the Mac, and the app has no network access.

Why does pasted code contain wrong characters?

OCR can confuse look-alike characters such as l and 1. Zoom in on small text before capturing, use Code mode, and compare the result with the image.

Get Optic on the Mac App Store · All Optic facts