Skip to main content

Incremental construction of command chains to replace text

I wrote some Elisp which lets you replace text by piping it through shell and region commands. So far so boring, Emacs does that out of the box. The interesting bit is that you build the command chain incrementally and get a live view of the current output.

The view is updated as you type, which allows you to experiment and adjust the current command, while checking the output. If you are satisfied with the result, you commit the command and can continue piping it into another one.

Finally, when you are done, just leave the input empty and the overall result is committed to the buffer, replacing the text you started with. You can of course quit at any time, which will restore the initial state.

I got the idea while playing with jq-mode and borrowed some of its code for this. Here is a screencast where I first experiment with tr by translating parentheses using $ tr "()" "[]", $ tr "()" "<>" and $ tr "()" " ".

After I commited the last one I pipe the result through upcase-region (-region can be ommitted) and rot13-region. Finally I pipe the current output to the command $ tac (cat reverted if you don’t know it) and commit the useful replacement to the buffer.

demo

If you are using objed you can call this command on any text object using |, otherwise you can used the linked code and use objed-ipipe as a regular region command.