Elisp: Write Emacs Commands Using Python, Ruby, …

By Xah Lee. Date: . Last updated: .

Write a script in your favorite language and make it into a emacs command!

Here's the elisp wrapper:

;; emacs wrapper to a script in python, ruby, etc.

(defun do-something-region (startPos endPos)
  "Do some text processing on region.
This command calls the external script “wc”."
  (interactive "r")
  (let (cmdStr)
    (setq cmdStr "/usr/bin/wc") ; full path to your script
    (shell-command-on-region startPos endPos cmdStr nil t nil t)))

In the above, just replace the /usr/bin/wc to the path of your script. You can include arguments as part of the command string.

Your script will need to:

The elisp function shell-command-on-region above, takes a region begin/end position, call your scritpt and passing the text as Stdin, gets the script's Stdout, and replace the region.

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

Passing Text to STDIN and Command Line Arg

In this example, we also pass a argument to the shell command.

You can simply do that by passing it as part of the command string.

(defun my-call-script-xyz ()
  "example of calling a external command.
passing text of region to its stdin.
and passing current file name to the script as arg.
replace region by its stdout."
  (let ((cmdStr
          "/usr/bin/python /home/joe/pythonscriptxyz %s"
    (shell-command-on-region (region-beginning) (region-end) cmdStr nil "REPLACE" nil t)))

2013-11-29 thanks to https://www.facebook.com/jrdngrnbrg for python tips.

Simple Example with JavaScript node.js

Elisp: Calling External Command to Decode URL Percent Encoding

Emacs Text Transform Under Cursor

  1. elisp wrapper + Python Ruby …
  2. Toggle Letter Case
  3. Title Case
  4. Upcase Sentences
  5. Cycle Space Hyphen Underscore
  6. Escape Quotes
  7. Quote Lines
  8. Spaces to New Lines
  9. Change Brackets/Quotes
  10. Remove Accent Marks
  11. Convert Straight/Curly Quotes
  12. Convert English/Chinese Punctuations
  13. Color Conversion (RGB, HSL, HSV)
  14. Decimal to Hexadecimal
  15. Replace Greek Letter Names to Unicode
  16. Twitterfy Text
  17. Toggle line wrap
  18. Clean White Space

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp