I am using comment-dwim-line to comment out multiple lines. For this, I automatically move cursor always to the beginning of a line. I just want to use this behavior if the cursor is at beginning of a line or multiple line is selected.

If the cursor is in the middle of a line, I just want to add a comment character where the cursor is located.

The code I am using:

(defun comment-dwim-line (&optional arg)
  "Replacement for the comment-dwim command.
        If no region is selected and current line is not blank and we are not at the end of the line,
        then comment current line.
        Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
  ;; http://www.opensubscriber.com/message/[email protected]/10971693.html
  (interactive "*P")
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (line-beginning-position) (line-end-position))
    (comment-dwim arg)))

(global-set-key (kbd "C-c C-;") 'comment-dwim-line)

Here it is possible to apply normal comment out behavior if the cursor is not at the beginning of a line:

def main():
    x = 10   
   |__ cursor is here when `comment-dwim-line` is applied it becomes

This is ok.

def main():
    # x = 10

But for the following example:

def main():
    x = 10   my commment
           |__ cursor is here when `comment-dwim-line` is applied

    # x = 10   my commment

It becomes:

def main():
    # x = 10  my commment

wanted behavior would be: x = 10 # my commment as:

def main():
    x = 10 # my commment

1 Answer 1


Experimentally, changing (line-beginning-position) to (point) in the arg list of comment-or-uncomment-region gives the behaviour you describe:

(defun comment-dwim-line (&optional arg)
  (interactive "*P")
  (if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
      (comment-or-uncomment-region (point) (line-end-position))
    (let ((comment-column (current-column)))
      (comment-dwim arg))))
  • This works close to what I wanted. But when I run this at the end of a line, it adds the comment character to very long distancle like: pid = os.getpid() # is this normal?
    – alper
    Feb 28, 2023 at 17:58
  • Sounds a bit strange, what would you want to be commented out in this circumstance? Feb 28, 2023 at 18:45
  • I just want # to be added to end of line right after the latest character but it adds . # instead of just #. Like if the cursor is at the end of the line and if there is no character after it ; it adds many empty space followed by #
    – alper
    Feb 28, 2023 at 20:55
  • I've edited the function so it doesn't add extra whitespace when at the end of a line (by temporarily setting comment-column) Feb 28, 2023 at 21:37
  • Thanks but the updated edit does still add extra whitespace in the case I try to explain :-(
    – alper
    Feb 28, 2023 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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