6

python-indent从 3 变成了 4。然后我mark-whole-bufferindent-for-tab-command. 它给了我垃圾。

4

6 回答 6

6

indent-region功能。所以我会尝试标记整个缓冲区,然后M-x输入indent-region. C-M-\据我所知,它通常绑定到。

编辑

重新缩进不适用于tab-width更改。正如我在评论中所写,将空格更改为制表符,然后更改tab-width是一个解决方案:

“猜想你是用空格而不是制表符缩进,你首先要tabify对缓冲区内容进行tab-width设置,并将其设置为 3。然后更改tab-width为 4 并运行untabify。”

于 2013-01-10T21:21:49.877 回答
2

这是一种黑客,但它不会给你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

这会将所有制表符转换为四个空格。

于 2013-01-10T21:29:11.383 回答
0

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.

于 2013-09-05T21:22:13.153 回答
0

尝试indent-region在缓冲区上。最初有界C-M-\

于 2013-01-10T21:23:29.320 回答
0

这也可能有帮助:

http://www.emacswiki.org/emacs/IndentingPython

特别是,PythonTidy对重组杂乱的代码非常有效,有一些小问题(不幸的是,该工具不容易配置):

http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc17

于 2014-02-13T15:41:36.867 回答
0

可能它不会有用或不是按主题,但我使用这样的脚本。从命令行运行它。(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()
于 2017-06-01T18:10:05.913 回答