I am using Emacs, Slime, Paredit, and other packages to work on Common Lisp (SBCL). It is really useful to comment out multiple expressions while debugging.

For instance, suppose I have these placeholder expressions:

    (      (  )
       ((   )
         (      (   )

I can use the keybinding C-M-SPC which run's the command mark-sexp on the second line. This character -!- will be used to represent the cursor position.

    (      (  )
       ((   )
         (      (   )

After invoking selecting the region, I can use the command comment-dwin to comment the appropriate expressions. Then, Emacs automatically does (using ParEdit):

    (      (  )
     -!-;; (
       ;; ((   )
       ;;   (      (   )
       ;;              )))

This is great. I just need to learn how to revert this after some editions inside the content of the expressions. Since there were other modifications, I cannot use undo. So, how can I remove the ;; comments in a smart way?

A friend mentioned something like rectangle-select but I could not find it.


In emacs, comment-dwim (bound to M-;) is its own inverse (AKA, involution):

Insert or realign comment on current line; if the region is active, comment or uncomment the region instead

I.e., to uncomment the commented region, select and activate the region and hit M-;.

If you want to avoid activating the region, C-u M-x comment-region RET will un-comment it.

PS1. Rectangle operations are documented in the manual: C-h i m Emacs RET m Rectangles RET.

PS2. See also https://stackoverflow.com/a/65649128/850781 for commenting parts of code.

  • 3
    FWIW, comment-region is (IMO) more flexible/useful than comment-dwim for both commenting and uncommenting multiple lines. It deals with nesting blocks of comments as one expects.
    – Drew
    Jul 20 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.