简体   繁体   中英

How to comment out current line in paredit?

I use M-; to comment out current line or comment out a region with mark sets before I meet paredit mode.

But in paredit mode, it seems that I can't comment out the current line unless I Ca and insert ; manually or mark the line before I M-; since in this mode use M-; in the beginning of line will insert ;;; above the line and in the middle or the end of line will insert a line ending comment.

So my question is how to comment out current line in a more elegant way?

I don't think paredit provides any command for commenting out a line, so I will share a tip that works regardless of whether one uses paredit or not.

Suppose you have this code:

(defun my-hi ()
  (dotimes (_ 2)
    (print "hello")
    (print (+ 1
              2))
    (print "world"))
  (print "the end"))

If you want to comment out the third line, you just select the expression and then press M-; . That is, you move point to any whitespace before the expression (print "hello") and then press CM-SPC to select the expression and then press M-; to comment out the expression.

What if you want to comment out two lines, for example, what if you want to comment out the expression (print (+ 1 2)) which span two lines? It's the same, you move point to any whitespace before the start of the expression, and then press CM-SPC M-; .

Now what if you want to comment out the line containing the expression (print "world") ? But that is a bad idea. Instead you want to comment out only the expression (print "world") . How to do that? Again it's the same.

What if you want to comment out all statements within the loop except for the first statement, that is, what if you want to comment out (print (+ 1 2)) and (print "hello") ? Move point before the start of the first expression that you want to comment out, and then press CM-SPC two times to select the two expressions and then press M-; .

Selecting expressions (as opposed to selecting lines) is not only safer (in the sense of not introducing mismatched parens), but also more convenient. For example, how would you comment out the dotimes form? Using CM-SPC to select the whole dotimes form is more convenient than selecting the five lines one by one.

The moral of the story here is similar to the following moral of Vim: "In Vim, the trick is you work with text objects (words, lines, sentences, code blocks, etc), not with letters."

In Lisp editing, the trick is you work with expressions (sexps), not with lines or letters.

To make things more convenient, you might want to define a command that comments out the first N expressions following point where N is specified by the number of times the command is called successively, or by prefix argument. Or a command that comments out one expression following point and moves point to start of the next expression, if any.

If you really want to comment out the current line, type Cq ; at the start of the line.

The problem is that that is almost never what you want.

Just type ; to comment out everything that can be commented on the line from the point on. (Any following closing delimiters will be moved to the next line.)

If trying to comment an empty line or the end of one just use ; with the cursor where the ; should be. When commenting out code one should consider commenting out an s-expression instead of a line. This way the balance of parentheses can be maintained. Paredit provides a useful interface for this.

The ; key can be used for lower level forms and they will be auto balanced by paredit, although I find it more ugly: (the cursor is | but not #| or |# )

(foo (bar | baz) quuz)

becomes:

(foo (bar ; baz
  ) quuz)

But

|(blah bla bl a)

produces an error .

If SLIME is installed then slime-insert-balanced-comments can be used which will happily insert a comment while maintaining balanced parentheses . This comment happens to be a read macro using #| and |# . There is always welcome to to Cq ; but it will make them unbalanced . The whole point of paredit is to keep them balanced.

(|defun (blah) blah) -> #|(defun (blah) blah)|#

It comments out the lowest level form possible:

(foo (bar | fuux) duux) -> (foo #|(bar | fuux)|# duux)

I have this in my .emacs.el file because slime-insert-balanced-comments is not bound to a key by default.

(global-set-key (kbd "C-c ;") 'slime-insert-balanced-comments)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM