Rares Vernica <rvernica@gmail.com> writes:
How can I set a global key binding to comment just one line, the
current line the cursor is?
I do not want to select the line and then M-;.
Just M-; on the current line will add a comment at the end of the line.
You can write a command that will do the line selection and the
commenting for you.
(defun comment-line ()
(interactive)
(save-excursion
(comment-region (progn (beginning-of-line) (point))
(progn (end-of-line) (point)))))
Then you can call it with M-x comment-line RET from anywhere on the line.
Or, you can additionnaly bind it to a global key.
Let's say: super-;
(global-set-key (kbd "s-;") (function comment-line))
Perhaps it would be useful to make it a toggle:
(defun toggle-comment-line ()
(interactive)
(save-excursion
(funcall
(if (progn (beginning-of-line)
(looking-at (format "\\s-*%s" (regexp-quote comment-start))))
(function uncomment-region)
(function comment-region))
(progn (beginning-of-line) (point))
(progn (end-of-line) (point)))))
(global-set-key (kbd "s-;") (function toggle-comment-line))