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

10.6: Format text via a Service and emacs System 10.6
Snow Leopard only hintOne very nice emacs feature is its text formatting functions. Combining the Snow Leopard Automator Services with emacs' scripting capabilities, one can easily, from any application, refill a paragraph to lines of 70 characters and indent list items:
  1. Open Automator, create a new Service.
  2. Set it up so the Service receives selected text from any application and replaces selected text.
  3. Add a new Run Shell Script action.
  4. Type in the script below, and save the Service.
tmp=/tmp/emacsmail`date "+%s"`.txt
cat /dev/stdin | tr '\r' '\n' > $tmp
/usr/bin/emacs -batch $tmp \
  -eval "(prefer-coding-system 'utf-8)" \
  -eval '(setq paragraph-start "\\-\\|$" paragraph-separate "$")' \
  -eval '(fill-region (point-min) (point-max) nil)' \
  -eval '(untabify (point-min) (point-max))' \
  -f save-buffer
cat $tmp

Provided that Aquamacs is installed, one can also edit text interactively with this version of the service:
tmp=/tmp/emacsmail`date "+%s"`.txt

cat /dev/stdin | tr '\r' '\n' > $tmp

/Applications/Aquamacs\\ Emacs \
  -eval "(prefer-coding-system 'utf-8)" \

cat $tmp
Hitting Control-X Control-S Control-X Control-C will quit Aquamacs and pipe its buffer in place of the previously selected text.

  • The script above can be adapted to tools other than Emacs. Yet, within Cocoa windows, text is usually UTF-8 with CR line breaks. Since some UNIX tools only detect LF breaks, you may need to filter text with tr '\r' '\n' | command | tr '\n' '\r'.
  • Hot keys can easily be assigned to services: System Preferences » Keyboard » Keyboard Shortcuts Tab » Services Menu.
[robg adds: I haven't tested this one.]
  • Currently 1.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (12 votes cast)

10.6: Format text via a Service and emacs | 4 comments | Create New Account
Click here to return to the '10.6: Format text via a Service and emacs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Format text via a Service and emacs
Authored by: d1taylor on Oct 28, '09 08:29:13AM

You're making this WAYYY too hard, chaps. Just use the "fmt" command on the command line. As an added bonus, you can specify how wide you want your lines to be too. For example:

fmt -w70

does what you want without the overhead of loading the emacs behemoth. Turns out that fmt can do a lot more if you read up on it. Try "man fmt" to get some of its slick tricks.

[ Reply to This | # ]
10.6: Format text via a Service and emacs
Authored by: 1amzave on Oct 28, '09 08:58:58AM
tmp=/tmp/emacsmail`date "+%s"`.txt
...would probably be cleaner via mktemp (1), and for this:
cat /dev/stdin | tr '\r' '\n' > $tmp
cat (1) reads stdin by default, so 'cat /dev/stdin' is redundant, though tr (1) also reads stdin by default, so the 'cat' is not actually needed (though if your system has it, 'mac2unix' may also be a slightly clearer way of achieving the same thing as that particular invocation of tr). In combination with fmt (1) as mentioned above, there should actually be no need for temporary file at all, obviating the 'mktemp' as well, and the script basically becomes a one-liner, perhaps just piping mac2unix into fmt (and maybe back into unix2mac again if you want carriage returns instead of line feeds).

I think the more fundamental hint here though is the mechanism of having text-manipulation scripts available as a service (and as mentioned, possibly with a quick hotkey) which is a cool idea that could allow lots of similarly-handy things.

[ Reply to This | # ]
10.6: Format text via a Service and emacs
Authored by: renaud on Jan 18, '10 04:34:46AM
You're making this WAYYY too hard, chaps. Just use the "fmt" command on the command line.
fmt is a nice formatting tool, but it won't do everything emacs does. For example, I don't believe that fmt would format a list like this:
- This is the first item of a list.

- This is the second item, in which the second line is properly
Edited on Jan 18, '10 04:35:13AM by renaud

[ Reply to This | # ]
Authored by: TGV on Oct 29, '09 05:02:52AM

Although this could indeed be done without using emacs (I was thinking of pr(1)), thanks for the hint. Pretty cool.

[ Reply to This | # ]