@Malabarba's answer looks like the simplest and most elegant
solution. However, if you do this enough that it warrants its
own function, you can also adapt comment-kill
to delete without
modifying the kill ring. Here's the source code of
comment-kill
with the single-line change to define comment-delete
:
(defun comment-delete (arg)
"Delete the first comment on this line, if any. Don't touch
the kill ring. With prefix ARG, delete comments on that many
lines starting with this one."
(interactive "P")
(comment-normalize-vars)
(dotimes (_i (prefix-numeric-value arg))
(save-excursion
(beginning-of-line)
(let ((cs (comment-search-forward (line-end-position) t)))
(when cs
(goto-char cs)
(skip-syntax-backward " ")
(setq cs (point))
(comment-forward)
;; (kill-region cs (if (bolp) (1- (point)) (point))) ; original
(delete-region cs (if (bolp) (1- (point)) (point))) ; replace kill-region with delete-region
(indent-according-to-mode))))
(if arg (forward-line 1))))
And here's a function (NB: minimally tested) that provides some additional functionality, allowing you to delete comments on the current line, in the active region, or in the entire buffer:
(defun comment-delete-dwim (beg end arg)
"Delete comments without touching the kill ring. With active
region, delete comments in region. With prefix, delete comments
in whole buffer. With neither, delete comments on current line."
(interactive "r\nP")
(let ((lines (cond (arg
(count-lines (point-min) (point-max)))
((region-active-p)
(count-lines beg end)))))
(save-excursion
(when lines
(goto-char (if arg (point-min) beg)))
(comment-delete (or lines 1)))))
I haven't checked performance issues, but perhaps there's a tiny
bump from not touching the kill ring. Regardless, I doubt you'll
notice performance problems unless you're working with a truly
massive buffer. But since you're unlikely to use this function
very frequently, it seems like it would not be worth the effort
to work on optimization.
M-x flush-lines ^\s-*\/\/
or something to that effect. Not perfect, but could work some times.