Comment or Uncomment One of More Lines

Artur Malabarba over at Endless Parentheses has a handy optimization for commenting out a single line or, perhaps, \(n\) lines at once. The usual procedure is to select the lines you want to comment and type 【Meta+;】 to call comment-dwim. I use that all the time but what if you want to comment (or uncomment) just one or perhaps a few lines?

Malabarba’s code comments/uncomments the current line or \(n\) lines if a numerical prefix is given. He binds it to 【Ctrl+;】 which gives it a nice symmetry with 【Meta+;】.

In the comments, Scott Turner suggests a version that calls comment-dwim if there is a region set and Malabarba’s code otherwise1. You could then just rebind【Meta+;】to the new version and have the best of both worlds2. Either way, if you find yourself commenting and uncommenting lines a lot, you may find this bit of Elisp handy.

Footnotes:

1

Be aware, though, that comment-dwim has its own default actions if there is no region. You may or may not care about them.

2

This, too, can be problematic. paredit, at least, also remaps 【Meta+;】.

This entry was posted in General and tagged . Bookmark the permalink.