我python-indent
从 3 变成了 4。然后我mark-whole-buffer
和indent-for-tab-command
. 它给了我垃圾。
6 回答
有indent-region
功能。所以我会尝试标记整个缓冲区,然后M-x
输入indent-region
. C-M-\
据我所知,它通常绑定到。
编辑
重新缩进不适用于tab-width
更改。正如我在评论中所写,将空格更改为制表符,然后更改tab-width
是一个解决方案:
“猜想你是用空格而不是制表符缩进,你首先要tabify
对缓冲区内容进行tab-width
设置,并将其设置为 3。然后更改tab-width
为 4 并运行untabify
。”
这是一种黑客,但它不会给你indent-region
正在给你的垃圾
1) 确保将制表符作为空格设置为 4 个空格。在暂存缓冲区类型中:
(setq tab-width 4)
然后通过标记和使用来评估它M-x eval-region
2)用制表符全局替换所有三个空格的集合
M-x replace-regexp [SPC][SPC][SPC][RET] C-q[TAB][RET]
3) 突出显示整个缓冲区并取消制表符
M-x mark-whole-buffer M-x untabify
这会将所有制表符转换为四个空格。
This is a bit of a hack, but it worked for me as a quick work-around: to a "M-X replace-string", " " -> " ". Then you have to close and re-open if your emacs does an automatic idnent-detection on the file. Then you have to go through and fix mult-line code (with tab), and strings that have lots of spaces.
尝试indent-region
在缓冲区上。最初有界C-M-\
这也可能有帮助:
http://www.emacswiki.org/emacs/IndentingPython
特别是,PythonTidy对重组杂乱的代码非常有效,有一些小问题(不幸的是,该工具不容易配置):
http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc17
可能它不会有用或不是按主题,但我使用这样的脚本。从命令行运行它。(python reindent.py some.py)
更改 string_equal 和 replace_to。
import sys
file_name = sys.argv[1]
string_equal = " "
replace_to = " "
with open(file_name) as f:
data = f.readlines()
f.close()
def create_new_line(i):
new_line = ""
flag = True
cur_s = ""
for k in i:
if flag and k == " ":
cur_s += k
if cur_s == string_equal:
new_line += replace_to
cur_s = ""
else:
flag = False
new_line += k
return new_line
with open(file_name, "w") as f:
for i in data:
l = create_new_line(i)
f.write(l)
f.close()