How can I apply M-;
to comments out blocks in matlab-mode? Currently it only adds comments at the end of line.
Thanks!
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)))
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
;; 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)
C-h k M-;
while in the *scratch* buffer and while in a buffer with matlab-mode
.
Commented
Nov 3, 2015 at 0:34
with-eval-after-load
was introduced in emacs 24.4. See if you can upgrade the emacs version on your machine (latest as of today is version 24.5). If not, I have updated the solution with an alternative (commented out) to use eval-after-load
instead of with-eval-after-load
for emacs 24.3 or older versions.
Commented
Nov 3, 2015 at 4:02
AFAIK, all you should need is:
(add-hook 'matlab-mode-hook (lamba () (local-set-key "\M-;" nil)))
so as to stop matlab-mode from overriding the default M-; binding. And please report this problem to the matlab-mode authors so they can fix it.
https://github.com/redguardtoo/evil-nerd-commenter
supports even emacs23.4, all you need is just one line setup:
(evilnc-default-hotkeys)
There many commands you can use, but if you want to comment out blocks separated by empty line, you just need M-x evilnc-comment-or-uncomment-paragraphs