start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

5.21. Tips for Copy and Paste Between Windows

One of my favorite uses for an xterm (which may seem natural to people who've grown up using window systems, but was a pleasant surprise for a guy who started computing with teletypes in 1970) is using a window to accept text pasted from some other window. For instance, in writing this book, I'll have one window open with something happening that I want to put into the book. So I select the text, then paste it into another xterm window -- where there's usually a text editor (like vi, with its keymaps for pasting text (Section 18.5)).

You can also use a text editor or Unix utilities to reformat text from one window before pasting it into another. For instance, you'd like to send most of the text in your browser to another window where you're composing an email message. But the web site used those irritating Microsoft Windows-specific quote characters that show up as question marks (?) on any other platform. So you paste the text into an Emacs window, do a quick run of text substitution, and copy the result to paste into the email window.

Figure Go to http://examples.oreilly.com/upt3 for more information on: requote

Another problem with email messages comes when you're sending a reply to someone who's used very long or jagged lines and the quoted text is a mess. But if you cut the messy text into an xterm window running the requote shell script, you'll get a neatened version. In the following example, the text I paste (cut from a quoted email message) is shown in boldface. Then I press CTRL-d, and the result appears; I can paste it back into the email message:

$ requote
> This is a long line of text that runs on and on and wraps to the next
line without a quote character at the start and it goes on and on and on
and well you know
> This is the next line of text
CTRL-d
> This is a long line of text that runs on and on and wraps to the next
> line without a quote character at the start and it goes on and on and
> on and well you know This is the next line of text

You can pass a fmt width option to tell requote how wide to make the output lines. (Different versions of fmt have different width options: -w, -l, etc.) requote also works great as a vi filter-through (Section 17.18): paste the messy text into vi, and run a command like !{requote to requote the text in place.

requote is a simple script that doesn't try to handle multiple levels of quoting (>> > >>, etc.). The main formatting commands are shown here; the temporary file $temp makes sure fmt has read all the text before the final sed outputs any of it:

${1+"$@"} Section 36.7

sed 's/^> //' |
fmt ${1+"$@"} > $temp
sed 's/^/> /' $temp

Here's another problem like the one requote solves. When I copy text from a browser window, my browser usually puts some whitespace before each line. When I paste the text, it's a mess. I could use a text editor to clean up the lines, but a one-line sed script can do the job faster.

Let's look at three examples of dedent. It removes all space and TAB characters from the start of each line it reads on its standard input, and it writes the result to standard output.

$ dedent > order_confirmation
   ...paste text into xterm, press CTRL-d...
$ dedent | fmt > johnson
   ...paste text into xterm, press CTRL-d...
$ dedent | mail -s 'article I mentioned' ali
   ...paste text into xterm, press CTRL-d...
$

In the first example, I started dedent and pasted text into the xterm. After I pressed CTRL-d, dedent removed leading whitespace from the pasted text and wrote the result to standard output, which the shell had redirected to a file named order_confirmation. In the second example, dedent's output is piped to fmt (Section 21.2) to make the lines neat. (Without dedent, most versions of fmt would indent the reformatted text.) The third example removes leading whitespace, then emails (Section 1.21) the text to ali.

One more thing: many of the tricks discussed earlier may be implemented as shell functions or even emacs functions or vi macro. If you use a mail user agent such as mutt, you can specify your favorite editor for email messages and just call the functions or macros while you edit. This is how I requote my replies to others' email, wrap it to a sane width, and so on. In emacs, ESC q is mapped to the function fill-paragraph, so if I need a paragraph wrapped to a certain width (determined by default-fill-column), I just position the cursor inside the paragraph and call the function. If the fill-prefix variable is properly set (say, to >) it even knows how to wrap several levels of nested quoting in email.

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.