How can I apply
M-; to comments out blocks in matlab-mode? Currently it only adds comments at the end of line.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
I prefer to use a generic "comment/uncomment line or region DWIM" kind of function that works in any major mode.
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)))
;; 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 ;; of `with-eval-after-load'. Note that `eval-after-load' accepts ;; only a single quoted form. ;; (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)
supports even emacs23.4, all you need is just one line setup:
There many commands you can use, but if you want to comment out blocks separated by empty line, you just need