Emacs set spacing for inline (end of line) comments
Asked Answered
R

4

8

In the PEP 8 style guide for python, it is recommended that inline comments are separated by the rest of the line by two spaces. However, the default in Emacs is that running comment-dwim or indent-for-comment puts only one space between the end of the line and the comment. Is there a way to change this default behavior in emacs?

I am running Emacs 23.3.1

Redolent answered 17/2, 2013 at 20:50 Comment(0)
C
6

This should do what you want:

   (add-hook 'python-mode-hook
      (lambda () (set (make-local-variable 'comment-inline-offset) 2)))
Calebcaledonia answered 22/11, 2014 at 2:35 Comment(0)
K
3

You can check emacs's documentation by C-h v RET comment-inline-offset, then you will find the answer as @And said.

Here's a simplified version:

(add-hook 'python-mode-hook
  (lambda () (setq-local comment-inline-offset 2)))
Keeleykeelhaul answered 8/4, 2016 at 15:14 Comment(0)
C
1

Try setting comment-start to " # " (one space before, one afters).

M-x set-variable comment-start " # "
Cloaca answered 17/2, 2013 at 21:38 Comment(2)
This somewhat works, but has two problems: First, it changes the commenting syntax for all lines (so when I comment out a line, they get commented with an extra space, un-aligning them with the rest of the text). Second, if i run comment-dwim on the line with the inline comment again, it re-aligns the inline comment to have only one space.Redolent
Yea, I understand. I had started digging through source last night, trying to find where the first space is inserted, but ran out of time.Cloaca
P
1

I think this might do what you want:

(defun my-comment-indent ()
  (interactive)
  (end-of-line)
  (let ((comment-column (+ 2 (current-column))))
    (comment-indent)))
Paraprofessional answered 18/2, 2013 at 5:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.