Emacs批量缩进Python [英] Emacs bulk indent for Python
问题描述
在Emacs中使用Python,如果我想添加一个try / except到一个代码块,我经常发现我必须逐行缩进整个块。在Emacs中,如何一次缩进整个块?
Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you indent the whole block at once.
我不是一个经验丰富的Emacs用户,只是发现它是通过ssh工作的最佳工具。我在命令行(Ubuntu)上使用Emacs,而不是gui,如果有任何区别。
I am not an experienced Emacs user, but just find it is the best tool for working through ssh. I am using Emacs on the command line(Ubuntu), not as a gui, if that makes any difference.
推荐答案
如果你使用Emacs编程Python,那么你应该使用python模式。使用python模式,在标记代码块之后,
If you are programming Python using Emacs, then you should probably be using python-mode. With python-mode, after marking the block of code,
Cc>
或 Cc Cl
将区域向左移动4个空格
C-c >
or C-c C-l
shifts the region 4 spaces to the right
Cc <
code> Cc Cr
将区域向左移动4个空格
C-c <
or C-c C-r
shifts the region 4 spaces to the left
如果需要将代码转换为两个级别的缩进,或一些任意数额,你可以在命令前加一个参数:
If you need to shift code by two levels of indention, or some arbitary amount you can prefix the command with an argument:
Cu 8 Cc>
移动区域8个空格在右边
C-u 8 C-c >
shifts the region 8 spaces to the right
Cu 8 Cc <
将区域向左移动8个空格
C-u 8 C-c <
shifts the region 8 spaces to the left
另一个选择是使用 Mx indent-rigidly
绑定到 Cx TAB
:
Another alternative is to use M-x indent-rigidly
which is bound to C-x TAB
:
Cu 8 Cx TAB
将区域向右移动8个空格
C-u 8 C-x TAB
shifts the region 8 spaces to the right
Cu -8 Cx TAB
将区域向左移动8个空格
C-u -8 C-x TAB
shifts the region 8 spaces to the left
还有用的是。
Also useful are the rectangle commands that operate on rectangles of text instead of lines of text.
例如,标记矩形区域后,
For example, after marking a rectangular region,
Cx ro
插入空格以填充矩形区域(有效地向右移动代码)
C-x r o
inserts blank space to fill the rectangular region (effectively shifting code to the right)
Cx rk
杀死矩形区域(有效地将代码移动到左边)
C-x r k
kills the rectangular region (effectively shifting code to the left)
Cx rt
提示输入一个字符串来替换矩形用。输入 C-u 8< space>
然后将输入8个空格。
C-x r t
prompts for a string to replace the rectangle with. Entering C-u 8 <space>
will then enter 8 spaces.
PS。使用Ubuntu,要使python-mode成为所有.py文件的默认模式,只需安装 python-mode
软件包。
PS. With Ubuntu, to make python-mode the default mode for all .py files, simply install the python-mode
package.
这篇关于Emacs批量缩进Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!