ChatGPT解决这个技术问题 Extra ChatGPT

Python 的 Emacs 批量缩进

在 Emacs 中使用 Python 如果我想在代码块中添加 try/except,我经常发现我不得不逐行缩进整个块。在 Emacs 中,如何一次缩进整个块。

我不是一个有经验的 Emacs 用户,但只是发现它是通过 ssh 工作的最佳工具。我在命令行(Ubuntu)上使用 Emacs,而不是作为 gui,如果这有什么不同的话。

C-c > 对。 C-c < 为左

u
unutbu

如果您正在使用 Emacs 编写 Python,那么您可能应该使用 python-mode。使用python模式,标记代码块后,

C-c >C-c C-l 将区域向右移动 4 个空格

C-c <C-c C-r 将区域向左移动 4 个空格

如果您需要将代码移动两个缩进级别,或者一些任意数量,您可以在命令前加上一个参数:

C-u 8 C-c > 将区域向右移动 8 个空格

C-u 8 C-c < 将区域向左移动 8 个空格

另一种选择是使用绑定到 C-x TABM-x indent-rigidly

C-u 8 C-x TAB 将区域向右移动 8 个空格

C-u -8 C-x TAB 将区域向左移动 8 个空格

对文本矩形而不是文本行进行操作的 rectangle commands 也很有用。

例如,标记一个矩形区域后,

C-x r o 插入空白以填充矩形区域(有效地将代码向右移动)

C-x r k 杀死矩形区域(有效地将代码向左移动)

C-x r t 提示输入一个字符串来替换矩形。输入 C-u 8 <space> 将输入 8 个空格。

PS。对于 Ubuntu,要使 python-mode 成为所有 .py 文件的默认模式,只需安装 python-mode 包。


谢谢,效果很好。使用 Emacs22 不会自动启用所有 .py 文件的 python 模式吗?无论如何,抄送 > 工作得很好。
@Vernon:C-c > 是在 python-mode.el 中定义的,所以我认为您一定是在某个地方安装了 python-mode 包。很高兴它对你有用。
Cc > 在 Emacs 23.2 中运行良好,无需安装 python-mode,因为它与提供的 python.el 一起使用
@landstatic:感谢您提供的信息。这让我觉得也许上面列出的所有命令对 python-mode.el 或 python.el 都一样?
@redobot:C-c C-lC-c C-r 必须是您自己设置的自定义绑定。如果您运行 emacs -q(在不加载 init 文件的情况下运行 emacs),您将看到在 Python 模式下没有对 C-c C-lC-c C-r 的绑定。
D
Daniel Stutzbach

除了默认映射到 C-M-\indent-region 之外,矩形编辑命令对 Python 非常有用。将区域标记为正常,然后:

Cx rt (string-rectangle):将提示您输入要插入每行的字符;非常适合插入一定数量的空格

cx rk(kill-rectangle):删除一个矩形区域;非常适合去除缩进

您也可以C-x r y (yank-rectangle),但这很少有用。


Ł
Łukasz

indent-region 映射到 C-M-\ 应该可以解决问题。


这是一个糟糕的建议。无法推断 Python 缩进(因为它是语法!)所以 ident-region 没用。
S
Singletoned

我一直在使用这个函数来处理我的缩进和取消缩进:

(defun unindent-dwim (&optional count-arg)
  "Keeps relative spacing in the region.  Unindents to the next multiple of the current tab-width"
  (interactive)
  (let ((deactivate-mark nil)
        (beg (or (and mark-active (region-beginning)) (line-beginning-position)))
        (end (or (and mark-active (region-end)) (line-end-position)))
        (min-indentation)
        (count (or count-arg 1)))
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
        (add-to-list 'min-indentation (current-indentation))
        (forward-line)))
    (if (< 0 count)
        (if (not (< 0 (apply 'min min-indentation)))
            (error "Can't indent any more.  Try `indent-rigidly` with a negative arg.")))
    (if (> 0 count)
        (indent-rigidly beg end (* (- 0 tab-width) count))
      (let (
            (indent-amount
             (apply 'min (mapcar (lambda (x) (- 0 (mod x tab-width))) min-indentation))))
        (indent-rigidly beg end (or
                                 (and (< indent-amount 0) indent-amount)
                                 (* (or count 1) (- 0 tab-width))))))))

然后我将它分配给键盘快捷键:

(global-set-key (kbd "s-[") 'unindent-dwim)
(global-set-key (kbd "s-]") (lambda () (interactive) (unindent-dwim -1)))

C
Community

我是 Emacs 新手,所以这个答案可能几乎没用。

到目前为止提到的答案都没有涵盖像 dictlist 这样的文字的重新缩进。例如 M-x indent-regionM-x python-indent-shift-right 和 company 如果您剪切并粘贴了以下文字并需要明智地重新缩进,则不会提供帮助:

    foo = {
  'bar' : [
     1,
    2,
        3 ],
      'baz' : {
     'asdf' : {
        'banana' : 1,
        'apple' : 2 } } }

感觉 M-x indent-region 应该在 python-mode 中做一些明智的事情,但(还)不是这样。

对于您的文字被括号括起来的特定情况,在有问题的行上使用 TAB 可以获得您想要的(因为空格不起作用)。

所以我在这种情况下一直在做的是快速记录一个 keyboard macro<f3> C-n TAB <f4> 一样在 F3、Ctrl-n(或向下箭头)、TAB、F4 中,然后重复使用 F4 应用宏可以保存一个几次击键。或者您可以执行 C-u 10 C-x e 来应用它 10 次。

(我知道这听起来并不多,但是尝试重新缩进 100 行垃圾文字而不会丢失向下箭头,然后必须向上 5 行并重复;))。


m
mookid

我使用以下代码段。在选择不活动的情况下,在选项卡上,它将当前行(如正常情况下)沉入; when the selection is inactive, it indents the whole region to the right.

(defun my-python-tab-command (&optional _)
  "If the region is active, shift to the right; otherwise, indent current line."
  (interactive)
  (if (not (region-active-p))
      (indent-for-tab-command)
    (let ((lo (min (region-beginning) (region-end)))
          (hi (max (region-beginning) (region-end))))
      (goto-char lo)
      (beginning-of-line)
      (set-mark (point))
      (goto-char hi)
      (end-of-line)
      (python-indent-shift-right (mark) (point)))))
(define-key python-mode-map [remap indent-for-tab-command] 'my-python-tab-command)

v
vineeshvs

以交互方式进行缩进。

选择要缩进的区域。 Cx 选项卡。使用箭头(<- 和 ->)以交互方式缩进。完成所需的缩进后,按 Esc 三次。

复制自我在 Indent several lines in Emacs 中的帖子


h
hpavc

我普遍做这样的事情

;; intent whole buffer 
(defun iwb ()
  "indent whole buffer"
  (interactive)
  ;;(delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

由于空格是 Python 语法的一部分,因此整个文件的缩进区域是个坏主意。