有没有办法通过按键在 emacs 中格式化完整的 python 缓冲区?

发布于 2024-08-02 01:13:48 字数 552 浏览 6 评论 0原文

我正在寻找通过按几个键让 Emacs 格式化 Python 缓冲区的任何方法。 就格式而言,我的意思是:

  1. 用 4 个空格替换制表
  2. 符 将所有长行正确换行为 79 个字符。 这包括包装和包装 连接长字符串、包装长注释、包装列表、函数头等。
  3. 不相关,但是当我按回车键时,如果光标自动插入,那就太好了。

一般来说,我只想根据 PEP 8 对所有内容进行格式化。

我已经为 Python 寻找了一个漂亮的打印机/代码美化器/代码格式化程序来运行缓冲区,但找不到开源的。

我的 .emacs 位于此处

对于那些要回答“Python 不需要格式化程序,这门语言的本质很美”的人,我告诉你,这是不正确的。 在真实的软件系统中,注释应该自动换行,字符串长度将超过 79 个字符,选项卡级别深度为 3+。 请直接帮助我解决我的问题,而不需要对格式化 Python 源代码的优点进行一些哲学讨论。

I am looking for any way to have Emacs format a Python buffer by hitting a few keys. By format, I mean:

  1. Replace tabs with 4 spaces
  2. Wrap all long lines correctly at 79 chars. This includes wrapping & concatenating long strings, wrapping long comments, wrapping lists, function headers, etc.
  3. Unrelated, but when I hit enter, it'd be nice if the cursor was tabbed in automatically.

In general I'd like to just format everything according to PEP 8.

I've looked for a pretty printer / code beautifier / code formatter for Python to run the buffer through, but can't find an open source one.

My .emacs is here.

For those who are going to answer "You don't need a formatter for Python, it's beautiful by the nature of the language," I say to you that this is not correct. In real software systems, comments should be auto-wrapped for you, strings are going to be longer than 79 characters, tab levels run 3+ deep. Please just help me solve my issue directly without some philosophical discussion about the merits of formatting Python source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清引 2024-08-09 01:13:48

要将制表符更改为空格并同时填充注释,您可以使用以下命令:

(defun my-format-python-text ()
  "untabify and wrap python comments"
  (interactive)
  (untabify (point-min) (point-max))
  (goto-char (point-min))
  (while (re-search-forward comment-start nil t)
    (call-interactively 'fill-paragraph)
    (forward-line 1)))

您可以将其绑定到您选择的键,大概如下所示:

(eval-after-load "python"
  '(progn
     (define-key python-mode-map (kbd "RET") 'newline-and-indent)
     (define-key python-mode-map (kbd "<f4>") 'my-format-python-text)))

注意 RET 键的设置为自动缩进。

如果您希望仅使用内置命令来使用带有空格的所有制表符,这是一个可能的顺序:

C-x h           ;; mark-whole-buffer
M-x untabify    ;; tabs->spaces

要使填充列和制表符宽度达到您想要的效果,请添加到您的 .emacs:

(setq fill-column 79)
(setq-default tab-width 4)

可以说,应该设置制表符宽度到 8,具体取决于其他人在您的环境中缩进代码的方式(8 是其他一些编辑器的默认值)。 如果是这种情况,您只需在 'python-mode-hook 中将其设置为 4 即可。 这有点取决于你的环境。

To change tabs into spaces and fill comments at the same time, you can use this command:

(defun my-format-python-text ()
  "untabify and wrap python comments"
  (interactive)
  (untabify (point-min) (point-max))
  (goto-char (point-min))
  (while (re-search-forward comment-start nil t)
    (call-interactively 'fill-paragraph)
    (forward-line 1)))

Which you can bind to the key of your choice, presumably like so:

(eval-after-load "python"
  '(progn
     (define-key python-mode-map (kbd "RET") 'newline-and-indent)
     (define-key python-mode-map (kbd "<f4>") 'my-format-python-text)))

Note the setting of the RET key to automatically indent.

If you wanted to all tabs with spaces with just built-in commands, this is a possible sequence:

C-x h           ;; mark-whole-buffer
M-x untabify    ;; tabs->spaces

To get the fill column and tab width to be what you want, add to your .emacs:

(setq fill-column 79)
(setq-default tab-width 4)

Arguably, the tab-width should be set to 8, depending on how other folks have indented their code in your environment (8 being a default that some other editors have). If that's the case, you could just set it to 4 in the 'python-mode-hook. It kind of depends on your environment.

鱼忆七猫命九 2024-08-09 01:13:48

关于你的第3点:

不相关,但是当我按回车键时,它会
如果光标处于 Tab 键状态就好了
自动。

显然,我的 emacs Python 模式默认执行此操作。 它只是简单地称为 python-mode...

About your point 3:

Unrelated, but when I hit enter, it'd
be nice if the cursor was tabbed in
automatically.

My emacs Python mode does this by default, apparently. It's simply called python-mode...

旧人九事 2024-08-09 01:13:48

使用 Spacemacs 的 Python 层 使用默认绑定:

序列 , = 将运行 yapfify-buffer 以在整个缓冲区上强加 PEP8 格式。

您可以设置

(setq-default dotspacemacs-configuration-layers '(
  (python :variables python-enable-yapf-format-on-save t)))

为在保存时启用自动重新格式化。

Using Spacemacs' Python layer with default bindings:

The sequence , = will run yapfify-buffer to impose PEP8 formatting on the entire buffer.

You can set

(setq-default dotspacemacs-configuration-layers '(
  (python :variables python-enable-yapf-format-on-save t)))

to enable automating reformatting on save.

七分※倦醒 2024-08-09 01:13:48

避免文件中出现制表符的最佳方法是根本不插入它们。 以下语句将始终插入正确数量的空格,而不是插入制表符

;; Turn off tab insertion in favor of space insertion
(setq-default indent-tabs-mode nil)

我看到您的 .emacs 文件中已经有这个

The best way to avoid tab characters in your file is to not insert them at all. The following statement will always insert the correct number of spaces, rather than inserting tab characters

;; Turn off tab insertion in favor of space insertion
(setq-default indent-tabs-mode nil)

I see you have this in your .emacs file already

可可 2024-08-09 01:13:48

autopep8 及其 emacs 包装器应该可以满足您的愿望。 https://github.com/ paetzke/py-autopep8.el

The autopep8 and the emacs wrapper for it should fulfill your desire.. https://github.com/paetzke/py-autopep8.el

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文