【问题标题】:Emacs bulk indent for PythonPython 的 Emacs 批量缩进
【发布时间】:2011-02-04 19:39:32
【问题描述】:

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

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

【问题讨论】:

  • C-c > 代表正确。 C-c <

标签: python emacs ssh


【解决方案1】:

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

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 模式吗?无论如何,C-c > 工作得很好。
  • @Vernon: C-c > 是在 python-mode.el 中定义的,所以我想你一定在某个地方安装了 python-mode 包。很高兴它对你有用。
  • C-c > 在 Emacs 23.2 中运行良好,无需安装 python-mode,因为它与提供的 python.el 一起使用
  • @landstatic:感谢您提供的信息。这让我觉得也许上面列出的所有命令对 python-mode.el 或 python.el 都一样?
  • @redobot: C-c C-lC-c C-r 必须是您自己设置的自定义绑定。如果你运行emacs -q(在不加载初始化文件的情况下运行emacs)你会看到C-c C-lC-c C-r在Python模式下没有绑定。
【解决方案2】:

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

  • C-x r t (string-rectangle):将提示您输入要插入每一行的字符;非常适合插入一定数量的空格
  • C-x r k(kill-rectangle):移除一个矩形区域;非常适合去除缩进

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 这是一个糟糕的建议。无法推断 Python 缩进(因为它是语法!)所以 ident-region 没用。
    【解决方案4】:

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

    (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)))
    

    【讨论】:

      【解决方案5】:

      我是 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、Ctrl-n(或向下箭头)、TAB、F4 中一样 &lt;f3&gt; C-n TAB &lt;f4&gt;,然后重复使用 F4 来应用宏可以节省几次击键。或者你可以C-u 10 C-x e申请10次。

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

      【讨论】:

        【解决方案6】:

        我使用以下sn-p。在选项时选中在选项卡上,它会缩小当前行(通常为它);选择不活动时,它会缩小右侧的整个区域。

        (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)
        

        【讨论】:

          【解决方案7】:

          以交互方式进行缩进。

          1. 选择要缩进的区域。
          2. C-x TAB.
          3. 使用箭头(->)以交互方式缩进。
          4. 完成所需的缩进后,按 Esc 三次。

          从我的帖子中复制:Indent several lines in Emacs

          【讨论】:

            【解决方案8】:

            我经常做这样的事情

            ;; 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 语法的一部分,整个文件的缩进区域是个坏主意。
            猜你喜欢
            • 2016-10-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多