Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Convert Clipboard contents to plain text UNIX
There was a recent article on the Macworld main site about apps you can buy that will strip formatting from text on the clipboard, leaving you with plain text that you can paste into documents without it having the wrong font, etc.

Although some of them do this automatically, I thought it's a bit much to pay for such a simple utility that can be done in one line on the CLI.

So here's a shell script that will convert the contents of the clipboard to plain text.
#!/bin/bash
#
# Convert contents of clipboard to plain text.

pbpaste | textutil -convert txt -stdin -stdout -encoding 30 | pbcopy

exit 0
The encoding option specifies MacRoman (30). I find that the default, UTF-8 doesn't always cope well with things like curly quotes.

If you have the AppleScript script menulet active, you can store the script in /Library/Scripts, and you can select it from the menu. It will run without opening the Terminal, as per this hint.

[crarko adds: This seems to work as described; I had to run the script twice before it did though.]
    •    
  • Currently 3.64 / 5
  You rated: 5 / 5 (11 votes cast)
 
[7,833 views]  

Convert Clipboard contents to plain text | 15 comments | Create New Account
Click here to return to the 'Convert Clipboard contents to plain text' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Convert Clipboard contents to plain text
Authored by: paulw on Jan 17, '12 07:38:55AM

Quicksilver has this built in as well, in the action "Paste As Plain Text"



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: luomat on Jan 17, '12 07:46:04AM
pbpaste -Prefer txt | pbcopy

seems to work fine for me.



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: pauljlucas on Jan 17, '12 01:10:28PM
The -Prefer txt option is the default, so you don't need to specify it.
---
- Paul


[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: kaltree on Jan 17, '12 07:48:57AM

I've always just opened the spotlight search field (CMD+SPACE), then pasted the text into the search field, then (CMD+A) to select all the text in the text field and (CMD+X) to cut the text into the clipboard.

It's funny that I rarely use spotlight for actually trying to find anything, but I use it for this and to perform basic calculations.

Thanks for sharing,
Kevin



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: willgonz on Jan 17, '12 08:04:28AM

I have done something similar but with objects, but I programmed mine. Is there a way to do this with, say, copying an image and pasting its hexadecimal values into program code like xcode or something else?



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: ctierney on Jan 17, '12 08:22:44AM

This applescript should do the same:
set the clipboard to «class ktxt» of ((the clipboard as text) as record)



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: kiltbear on Jan 17, '12 10:22:06AM

The other option is under the edit menu choose either "Paste as Plain Text" or "Paste and Match Style". I think have seen it implemented as both.

In some apps (Mail) I have remapped cmd-V to be "Paste and Match Style" rather than "Paste". (it does have some side affects, like it breaks pasting via keyboard into subject, and address lines).

So when I want to paste and preserver formatting from the original, I can choose to "right click" in the mail text area and choose paste.



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: joelseph on Jan 17, '12 12:33:45PM

I have switched the mapping for "Paste" and "Paste and Match Style" in Mail as well. I can still paste into subject fields, simply use the option-command-V key combo when I need to paste there. For my little brain, it's must less frustrating to try command-V in the subject and it not work... and then remember to add the option to the combo... than it was to paste formatted text into the body when I didn't intend to!



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: squalene on Jan 17, '12 11:25:25AM

Works nicely through 'FastScripts' menu for me, and I don't have to remember which App I'm in to use it.
It does strip about half of an emoji character 🎃 -> ??; but Lion support for emoji is generally still a little flaky.



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: szedula on Jan 17, '12 12:03:14PM

This old hint:

http://hints.macworld.com/article.php?story=20040204170653788

From '04, talks about the same thing but as an Applescript, but also mentions pbpaste and pbcopy.



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: pauljlucas on Jan 17, '12 01:13:16PM
I've always used:
#!/bin/sh
pbpaste | pbcopy
osascript -e 'tell application "System Events" to keystroke "v" using command down'
The osascript command actually does the paste.
---
- Paul


[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: wallybear on Jan 17, '12 02:13:14PM
There's an old hint about this. (and an older one from 2001)
Basically it suffices
pbpaste | pbcopy
as by default this operation copies the plain text to clipboard.


[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: drc_007 on Jan 18, '12 01:11:20AM
This tutorial maybe of interest Using PTHPasteboad Pro

[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: lar3ry on Jan 18, '12 12:38:39PM

I do something like this via Automator and stick it in the Services submenu--useful for those apps that don't have "Copy plain text" or whatever.



[ Reply to This | # ]
Convert Clipboard contents to plain text
Authored by: popguru on Jan 18, '12 04:46:15PM

I've used a script that just runs pbpaste | copy for years and wondered what the intent of the textutil in this hint was for. According to NSString docs, -encoding 30 corresponds to NSMacOSRomanStringEncoding. So this script will attempt to convert the text in the clipboard to Mac OS Roman. That means it also corrupts any unicode characters which cannot be expressed in Mac OS Roman (e.g. Japanese or Chinese characters).

Just wondering: Why would you want to convert the text to Mac OS Roman? Is this needed to paste in an application that expects Mac OS Roman rather than UTF-8?



[ Reply to This | # ]