<!-- language-all: lang-el -->

I prefer to use a generic "comment/uncomment line or region DWIM" kind of function that works in any major mode.

## Generic comment DWIM function

Here is the function I use:

    (defun endless/comment-line-or-region (n)
      "Comment or uncomment current line and proceed to the next line.
    With positive prefix, apply to N lines including current one.
    With negative prefix, apply to -N lines above.
    If region is active, apply to active region instead."
      (interactive "p")
      (if (use-region-p)
          (comment-or-uncomment-region
           (region-beginning) (region-end))
        (let ((range
               (list (line-beginning-position)
                     (goto-char (line-end-position n)))))
          (comment-or-uncomment-region
           (apply #'min range)
           (apply #'max range)))
        (forward-line 1)
        (back-to-indentation)))

## Tweaking the bindings for `matlab-mode`
    
    ;; Unbind the M-; key from `matlab-mode-map'
    ;; `with-eval-after-load' will work in emacs 24.4 and newer versions
    (with-eval-after-load 'matlab
      (define-key matlab-mode-map (kbd "M-;") nil)) 
 
    ;; For emacs 24.3 and older versions use `eval-after-load' instead
    ;; (eval-after-load 'matlab
    ;;   '(define-key matlab-mode-map (kbd "M-;") nil)) 

    ;; Define the M-; binding globally
    ;; This binding will be effective in any major-mode that does not
    ;; have that major-mode-specific M-; binding
    (global-set-key (kbd "M-;") #'endless/comment-line-or-region) 

---

[**Source**][1]

[1]: http://endlessparentheses.com/implementing-comment-line.html