I am experimenting with using org-mode for some of my startup config, and adding (org-babel-load-file (concat user-emacs-directory "config.org")) to my .emacs.

Then I use a structure like the following to embed elisp:

** Disable window clutter
#+begin_src emacs-lisp
(setq initial-scratch-message nil)
(setq inhibit-splash-screen t)

(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))

Inside the source block, M-x comment-region uses a '#' as the comment character. What is the best/easiest way to make comment-region adjust to the block it is in?


Use https://github.com/redguardtoo/evil-nerd-commenter, run M-x NUM evilnc-comment-or-uncomment-lines will comment/uncomment NUM lines under cursor.

Please note evil is not required for this command.

See https://github.com/redguardtoo/evil-nerd-commenter/blob/fa40dab8d2f010db17e1e62dfd245c1504d0542f/evil-nerd-commenter.el#L397 for key algorithm.

It's very simple. use org-edit-src-find-region-and-lang (Emacs 24.4+) or org-element-at-point (Emacs 26.1+) to find the the major mode for current code snippet.

Open a new temporary buffer, insert the code snippet, switch to correct major mode, comment out code, then replace the snippet in org file with the snippet in the temporary buffer.

Here is demo, enter image description here

| improve this answer | |

From: https://lists.gnu.org/archive/html/emacs-orgmode/2013-11/msg00318.html

#+BEGIN_SRC emacs-lisp :results none
;; allow comment region in the code edit buffer (according to language)
(defun my/org-comment-dwim (&optional arg)
  (interactive "P")
  (or (org-babel-do-key-sequence-in-edit-buffer (kbd "M-;"))
      (comment-dwim arg)))

;; make `C-c C-v C-x M-;' more convenient
(define-key org-mode-map
  (kbd "M-;") 'my/org-comment-dwim)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.