0

comment-dwim (commend-do-what-I-mean, usually M-;) works well. It:

  1. appends spaces at the end of the line until at least some column is reached,
  2. if the preceding line also had a comment tailing the code, aligns the comment character, and
  3. inserts the comment character for the mode, followed by one space.

In Python one feature is missing to comply with the stringent rules:

  1. at least two spaces must separate code from the trailing comment.

How can I configure comment-dwim to leave at least two spaces in python-mode?

Calaf
  • 10,113
  • 15
  • 57
  • 120
  • 1
    You can take a look at `comment-indent-function`, see https://stackoverflow.com/a/26316611/18118915. Or just `let` `comment-column` on the fly. – Tianshu Wang Mar 03 '22 at 08:13

1 Answers1

0

Fiddling with comment-dwim may be holding the wrong end of the stick. For me, M-; automatically inserts two spaces, the '#' character, and a space, following which point is placed. If it's not doing that for you, then you may have reconfigured it somewhere else, possibly accidentally.

I suggest an alternative is to have a 'format-on-save' option enabled, which will do this for you without fussing in your .emacs or with customize.

(use-package python-black :after python
    :hook (python-mode . python-black-on-save-mode)
    :bind ("C-c p C-f" . 'python-black-buffer))

I use black, but any alternative formatter should do the same. I never worry about how my file is formatted, because black fixes it for me automatically.

From the docs for black:

Black does not format comment contents, but it enforces two spaces   
between code and a comment on the same line, and a space before the 
comment text begins.
naugiedoggie
  • 300
  • 3
  • 10
  • Good to know. But since I also like to use flycheck while coding, I'd much rather if it's correct all along so I don't have to see squiglies all over, rather than just correct when saving. – Calaf Apr 02 '22 at 19:34