Python Mode provides its own fill-paragraph-function
, called python-fill-paragraph
. This function implements extensive formatting of code beyond just wrapping paragraphs.
Change the filling style
Notably, it formats docstrings according to python-fill-docstring-style
, whose default value is pep-257
, that is, formatting docstrings according to PEP 257 where the first sentence is one the same line with the opening quotes and a blank line is inserted before the closing quotes.
Your example shows a different docstring style. Change python-fill-docstring-style
to django
(setq python-fill-docstring-style 'django)
which is the style used in your example. Python Mode will still fill the entire docstring, but preserve your style of writing docstrings.
This is the setting I’d recommend.
Really fill the paragraph only
Alternatively, you can tell Python Mode to fill only the current paragraph when in a comment, by customizing python-fill-string-function
. This is the filling function that Python Mode uses when inside a string. The following setting makes Python Mode use the default filling behaviour in docstrings:
(setq python-fill-string-function (lambda (&optional justify region)
(let ((fill-paragraph-function nil))
(fill-paragraph justify region))))