Emacs Lisp – Block Commenting Ruby Code

These days I’m working to become proficient in Emacs (I’ve recently switched fromĀ Vim, as Greg mentioned on his blog). With that comes all kinds of things like org-mode, yasnippet, and lots of keyboard combinations.

A part of this current effort I’m learning Emacs Lisp right now. There’s probably a better way to do block comment cycling in Ruby, but this was good practice on writing some simple elisp functions.

(defun ruby-comment-region ()
  "In Ruby, comment out the current region with =begin/=end statements."
  (interactive)
  (save-excursion
    (goto-char (point))
    (insert "=end")
    (goto-char (mark))
    (insert "=begin\n")))

(defun ruby-uncomment-region ()
  "In Ruby, uncomment the current region -- remove =begin/=end statements."
  (interactive)
  (save-excursion
    (if (re-search-backward "^=begin")
        (replace-match ""))
    (if (re-search-forward "^=end")
        (replace-match ""))))

2 Responses Subscribe to comments


  1. HeresTomWithTheWeather

    judas!!

    Sep 04, 2008 @ 11:12 pm


  2. andy

    I know I know, it’s terrible. I still call it Escape-Meta-Alt-Ctrl-Shift. Of course, my VAX/VMS coworkers said the same thing when I left EDT for Vim!

    Sep 05, 2008 @ 7:25 am

Reply