Emacs has some nice facilities for interacting with external
processes. Recently I was working with an extremely long line with no
spaces that I wanted to split into lines of 72 characters. I banged
this out in about 20 seconds:
M-| (shell-command-on-region) perl -e '$_=<>;s/(.{72})/$1\n/g;print'
Now,
- Maybe I could have written this to a file and run perl on it using
the shell - Maybe I could have used a keyboard macro, something like
C-x ( C-u 7 2 C-f <RET> C-x ) C-x C-e e e e e ...
- perhaps
$_
<>= can be replaced with just<>
or an appropriate command line switch, reducing my character count by two - Maybe there is a function within emacs that does this already –
perhaps something similar to(let ((sentence-end "."))
(which doesn’t work)
(fill-paragraph nil))
However, interacting with the external process
a) worked first time and
b) made me feel clever
b) made me feel clever
and sometimes, particularly in this kind of job, that is the most important thing.
Advertisements
You could use replace-regexp, like this:
M-x replace-regexp[enter]
\(.\{72\}\)[enter]
\1[ctrl-q][ctrl-j][enter]
where the items in [] are keypresses. The regex is pretty much the same as what you have there in perl, except with more backslashes, and the “ctrl-q ctrl-j” thing is needed to get a newline into the replacement text.
Also, you could shorten your perl like this:
perl -pe ‘s/(.{72})/$1\n/g’
Lastly, I assume you used a prefix arg on your M-| too, since that would cause the perl output to replace the region directly.
Hi Steve,
Very useful information there. Emacs is pretty TMTOWTDI too eh? 🙂 And no, I didn’t use a prefix arg so it opened in another buffer but I will next time.
Thanks very much
[…] One thing that isn’t well highlighted (so to speak) is how well ediff drills down on the exact change. It doesn’t just say “this line is different to this line”, it focuses on the exact change within the line. You can easily see here the tags that have been added to an old revision of one of my earlier posts. […]