# Writing emacs commands with your fav lang

**URL:** <https://www.ruby-forum.com/t/writing-emacs-commands-with-your-fav-lang/151031>\
**Category:** Ruby\
**Created:** [October 16, 2008, 3:00am UTC](https://www.ruby-forum.com/t/writing-emacs-commands-with-your-fav-lang/151031 "2008-10-16T03:00:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2008, 3:00am UTC](https://www.ruby-forum.com/t/writing-emacs-commands-with-your-fav-lang/151031/1 "2008-10-16T03:00:50Z")

</div>

Here’s a little tutorial that lets you write emacs commands for  
processing the current text selection in emacs in your favorite lang.

Elisp Wrapper For Perl Scripts  
[http://xahlee.org/emacs/elisp\_perl\_wrapper.html](http://xahlee.org/emacs/elisp_perl_wrapper.html)

## plain text version follows.

Elisp Wrapper For Perl Scripts

Xah Lee, 2008-10

This page shows a example of writing a emacs lisp function that  
process text on the current region, by calling a external perl script.  
So that you can use your existing knowledge in a scripting language  
for text processing as emacs commands.

THE PROBLEM

Elisp is great and powerful, but if you are new, it may take several  
months for you to actually become productive in using it for text  
processing. However, you are probably familiar with a existing  
language, such as Perl, PHP, Python, Ruby. It would be great if you  
can use your existing knowledge to write many text processing scripts,  
and make them available in emacs as commands, so that you can just  
select a section of text, press a key, then the selected text will be  
transformed according to one of your script.

SOLUTION

Basically, all your elisp function has to do is to grab the current  
region, then pass the text to a external program. The external program  
will take the input thru Stdinâ†—, then produce the processed result in  
Stdout. The elisp function will grab the text from the script’s  
Stdout, then replace the current region by that text. Lucky for us,  
the elisp function shell-command-on-region already does this exactly.

For your script, its should takes input from Stdin and oput to Stdout.  
For simplicity, let’s assume your script is the unix program â€œwcâ€,  
which takes input from Stdin and output a text to Stdout. (the â€œwcâ€  
command counts the number of words, lines, chars in the text.) For  
example, try this: â€œcat â€¹file nameâ€º | wcâ€.

Here’s the elisp wrapper:

(defun my-process-region (startPos endPos) “Do some text processing on  
region. This command calls the external script â€œwcâ€.” (interactive  
“r”) (let (scriptName) (setq scriptName “/usr/bin/wc”) ; full path to  
your script (shell-command-on-region startPos endPos scriptName nil t  
nil t) ))

You can assign a keyboard shortcut to it:

(global-set-key (kbd “”) 'my-process-region)

Put the above code in your â€œ.emacsâ€ then restart emacs. To use your  
function, first select a region of text, then press the F6 key.

With the above, you can write many little text processing scripts in  
your favorite language, and have them all available in emacs as  
commands.

For how to define keyboard shortcuts with other keys, see: How to  
Define Keyboard Shortcuts in Emacs.

Xah  
âˆ‘ [http://xahlee.org/](http://xahlee.org/)

â˜„
