Skip to content

Instantly share code, notes, and snippets.

@jamescherti
Last active August 18, 2024 16:50
Show Gist options
  • Select an option

  • Save jamescherti/b8987b3299c0b567f1fdfffc1878a686 to your computer and use it in GitHub Desktop.

Select an option

Save jamescherti/b8987b3299c0b567f1fdfffc1878a686 to your computer and use it in GitHub Desktop.
Emacs: Enable multi-line commenting and indenting
;; Description: Enable multi-line commenting and indenting in Emacs
;; Gits URL: https://gist.github.com/jamescherti/b8987b3299c0b567f1fdfffc1878a686
;; License: MIT
;; Author: James Cherti
;; Enable multi-line commenting. This allows comments to span multiple lines,
;; which is useful for writing longer comments or docstrings.
(setq comment-multi-line t)
;; Bind the `RET` (Return) key to `comment-indent-new-line`.
;; This ensures that pressing Enter within a comment context will both insert a
;; new line and correctly indent it to match the comment's indentation level,
;; facilitating more consistent formatting of multi-line comments.
(global-set-key (kbd "RET") 'comment-indent-new-line)
;; Emacs Evil users: uncomment the following:
;; (with-eval-after-load "evil"
;; (defun my-evil-open-below ()
;; "Insert a new line above and indent it using `comment-indent-new-line'."
;; (interactive)
;; (end-of-line)
;; (comment-indent-new-line)
;; (evil-insert-state))
;;
;; (defun my-evil-open-above ()
;; "Insert a new line above and indent it using `comment-indent-new-line'."
;; (interactive)
;; (previous-line)
;; (my-evil-open-below))
;;
;; (defun my-evil-setup-comment-indent-new-line ()
;; "Rebind o to use `my-evil-open-below' in normal mode."
;; (define-key evil-normal-state-map (kbd "O") 'my-evil-open-above)
;; (define-key evil-normal-state-map (kbd "o") 'my-evil-open-below))
;;
;; (add-hook 'evil-mode-hook 'my-evil-setup-comment-indent-new-line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment