reddit / emacs

I learned how to comment out code in Emacs Lisp today

genakag · 2022-07-12 01:01
[https://blog.genenakagaki.com/en/how-do-i-comment-on-emacs-lisp.html](https://blog.genenakagaki.com/en/how-do-i-comment-on-emacs-lisp.html) The only way I knew how to comment code in Emacs Lisp was to insert \`;\` before the code I wanted to comment. Now I have a key binding that makes it easier for me to comment. This is my very first post, so please let me know if I'm doing something wrong!
fromadarkcontinent · 2022-07-12 01:17
It is a nice experiment to learn elisp but for the purpose of realworld use there is a builtin command in emacs called comment-dwim that is bound to "M-;" and it supports almost all programming and markup modes.
00-11 · 2022-07-12 01:18
*Ask Emacs!* https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html (Thanks for pointing to such references.) ---- FWIW, I use this, instead of `comment-or-uncomment-region`, `comment-dwim`, `comment-region`, ... (Of those, `comment-region` is the best, IMO. It handles nested comment blocks etc.) (defun comment-region-lines (beg end &optional arg) "Like `comment-region' (which see), but comment or uncomment whole lines." (interactive "*r\nP") (when (> beg end) (setq beg (prog1 end (setq end beg)))) (let ((bol (save-excursion (goto-char beg) (line-beginning-position))) (eol (save-excursion (goto-char end) (if (bolp) (point) (line-end-position))))) (comment-region bol eol arg))) I bind that to `C-x C-;`. `C-u` to uncomment. Numeric arg for different comment levels. And I use `M-x` as `comment-dwim` only for end-of-line, single-; comments.
[deleted] · 2022-07-12 01:49
[deleted]
dpoggio · 2022-07-12 02:56
Expected to read this comment.
genakag · 2022-07-12 03:26
Thanks for letting me know. I just tried it out, and it does what I mean! But for me it doesn't uncomment with the same command. How do you usually uncomment?
chris_thoughtcatch · 2022-07-12 03:56
M-x comment-or-uncomment-region
Schievel1 · 2022-07-12 04:28
C-x C-; crew unite!
00-11 · 2022-07-12 04:46
Right. One of the reasons it's inferior to `comment-region`, IMO. `comment-dwim` is fine for an end-of-line comment (one of the things it does). But it's limited when it comes to commenting and uncommenting a region of text - particularly commenting and uncommenting levels of nested comment blocks.
fromadarkcontinent · 2022-07-12 05:32
You just mark the region that is commented and press the same key binding. Be sure you mark only the region that is commented out. If there are line breaks that have no comment symbols it will try to comment everything again. If you want it to behave differently you can override it in mode specific manner.
xenodium · 2022-07-12 06:45
`comment-line`, despite its name, does all the right DWIM things for ya, bound it to `C-;`.
deaddyfreddy · 2022-07-12 08:20
(use-package smart-comment :ensure t :bind ("M-;" . smart-comment))
Nondv · 2022-07-12 08:20
I like that in clojure there's a quotation for commenting out whole s-expressions. Something like (do-something x y #_(not evaluated)) and no need to keep track of closing parens
ideasman_42 · 2022-07-12 11:07
Are there any commenting functionality/packages that handle s-expressions well? (defun example () (progn (foo) (bar))) Expected result with the cursor on the last line: (defun example () (progn (foo) ;; (bar) )) Manually having to move the parenthesis onto the next line can be tedious.
b3n · 2022-07-12 11:34
You could always define a comment macro in elisp, e.g. (defmacro // (&rest sexp) nil) Then you can do things like (do-something x y (//(not evaluated)))
redguardtoo · 2022-07-12 11:39
See https://github.com/redguardtoo/evil-nerd-commenter Non-evil users can still use it. My favorite technique is its integration of imenu, see https://github.com/redguardtoo/evil-nerd-commenter#use-imenu-to-list-and-jump-to-comments-in-current-file
[deleted] · 2022-07-12 11:42
[deleted]
mitchTux · 2022-07-12 11:57
You mean you guys don't do?: ``` (when nil I am a comment) ```
Nondv · 2022-07-12 12:17
yeah I've been thinking about it but the problem still persists bc i need to add an extra paren. if there's a way to define macros similar to quotations, that would work
genakag · 2022-07-12 13:23
I got it to work! I wasn't highlighting the code I wanted to uncomment. This works really nicely without any setup :D
genakag · 2022-07-12 13:29
If you can highlight the part that you want to comment, `comment-dwim(M-;)` works pretty nicely. I added a gif animation if you want to see how it looks. https://blog.genenakagaki.com/en/how-do-i-comment-on-emacs-lisp.html#org72fd0cf For my case, I use vim so I can highlight everything inside a parenthesis with `vab`
genakag · 2022-07-12 13:33
I love this comment syntax! I have it everywhere in my Clojure code just to leave example usages.
_noctuid · 2022-07-12 14:29
Lispy
paretoOptimalDev · 2022-07-12 17:17
I might use this because all I really do is comment blocks or lines and comment-dwim seems to be on another page as me.
b3n · 2022-07-12 22:37
Actually you don't need any extra paren, you could simply put `// ` immediately *after* the opening paren rather than before it. So it would look like (do-something x y (// not evaluated))
hungry_m8 · 2022-07-12 23:31
nice! compared to clojure and scheme, it's still a bit inconvenient to have to wrap what you want to comment out in parens. sometimes, you just want to comment out the sexp in front
dpoggio · 2022-07-12 23:46
Can you give one specific example of `comment-dwim` not working right when `comment-region` does? It’s quite weird, because it actually calls `comment-region` if needed.
dpoggio · 2022-07-12 23:52
Whenever a region is active (describe `use-region-p` for details), `comment-dwim` uses `comment-or-uncomment-region`, so it’s implied.
jets2 · 2022-07-13 05:30
`comment-line` has always worked well for me too! Good for commenting and uncommenting. Isn't its default binding `C-x C-;` though?
Nondv · 2022-07-13 07:54
oh yeah that's true!
00-11 · 2022-07-13 17:24
From `C-h f comment-dwim`: > (***unless it only consists of comments***, in which case it calls `uncomment-region`) Try nesting blocks of comments. Try unnesting them.
ilemming · 2022-07-14 19:24
Technically, `#_` reader macro in Clojure doesn't comment out code - the reader just ignores the sexp after it. So, it is slightly different than commenting out. e.g., try creating an "unbalanced" map: `{:foo}` - obviously, evaling this would throw an exception. Map literal must contain an even number of forms. now, if you comment it out: `;; {:foo}` - you can eval the code but if you do this: `#_{:foo}` - it won't work. Also, many don't know that you can ignore multiple sexps with that macro, i.e., #_#_#_(foo) (bar) (zap) (zop) First three will be ignored, but `(zop)` gets called. Very nice macro. I really hope someone ports it to Elisp.
Bonsai-Haiku · 2022-07-15 23:19
; ;; ;;; ;;;;…. There are more than one type of comments in elisp