如果您正在使用 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 TAB
的 M-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
包。
除了默认映射到 C-M-\
的 indent-region
之外,矩形编辑命令对 Python 非常有用。将区域标记为正常,然后:
Cx rt (string-rectangle):将提示您输入要插入每行的字符;非常适合插入一定数量的空格
cx rk(kill-rectangle):删除一个矩形区域;非常适合去除缩进
您也可以C-x r y
(yank-rectangle
),但这很少有用。
indent-region
映射到 C-M-\
应该可以解决问题。
我一直在使用这个函数来处理我的缩进和取消缩进:
(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)))
我是 Emacs 新手,所以这个答案可能几乎没用。
到目前为止提到的答案都没有涵盖像 dict
或 list
这样的文字的重新缩进。例如 M-x indent-region
或 M-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 行并重复;))。
我使用以下代码段。在选择不活动的情况下,在选项卡上,它将当前行(如正常情况下)沉入; 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)
以交互方式进行缩进。
选择要缩进的区域。 Cx 选项卡。使用箭头(<- 和 ->)以交互方式缩进。完成所需的缩进后,按 Esc 三次。
复制自我在 Indent several lines in Emacs 中的帖子
我普遍做这样的事情
;; intent whole buffer
(defun iwb ()
"indent whole buffer"
(interactive)
;;(delete-trailing-whitespace)
(indent-region (point-min) (point-max) nil)
(untabify (point-min) (point-max)))
不定期副业成功案例分享
C-c >
是在 python-mode.el 中定义的,所以我认为您一定是在某个地方安装了python-mode
包。很高兴它对你有用。C-c C-l
和C-c C-r
必须是您自己设置的自定义绑定。如果您运行emacs -q
(在不加载 init 文件的情况下运行 emacs),您将看到在 Python 模式下没有对C-c C-l
或C-c C-r
的绑定。