简体   繁体   English

重新格式化Python文件以具有4个空格缩进

[英]Reformat a Python file to have 4 space indentations

I'm editing a Python file that uses two spaces for programmatic indents - I prefer 4 spaces. 我正在编辑一个Python文件,它使用两个空格进行编程缩进 - 我更喜欢4个空格。 In my .vimrc I have the following settings related to indentation: 在我的.vimrc中,我有以下与缩进相关的设置:

set tabstop=4                     "Indentation levels every four columns
set expandtab                     "Convert all tabs typed to spaces
set shiftwidth=4                  "Indent/outdent by four columns
set softtabstop=4

How do I get Vim to convert all the existing 2 space indents to be 4 space indents? 如何让Vim将所有现有的2个空格缩进转换为4个空格缩进?

In other words: 换一种说法:

if something:
  dothis()

becomes

if something:
    dothis()

When I tried gg=G 当我尝试gg = G.

def check():
  for a in list:
    for  b in list2:
      check(a, b)
      while (len > MAX) :
        poll()

  while(len(thelist) > 0) :
    poll()  

  return results

became 成为

def check():
    for a in list:
    for  b in list2:
    check(a, b)
    while (len > MAX) : 
        poll()

        while(len(thelist) > 0) :
            poll()

            return results

In order to double the number of spaces at the beginning of every line (and only at the beginning): 为了使每行开头的空格数加倍(并且仅在开头):

:%s/^\s*/&&/g

& in replacement pattern is the matched pattern. &替换模式是匹配模式。

Probably it will not have any side-effect for you. 可能它对您没有任何副作用。

Pressing gg=G is the command to re-indent everything in a file. gg=G是重新缩进文件中所有内容的命令。 If you have other elements that can be re-indented, vim will indent these as well, which doesn't always give the desired effects. 如果你有其他可以重新缩进的元素,vim也会缩进这些元素,这并不能总是产生预期的效果。 You'll have to clean these up manually if they're ugly. 如果丑陋,你必须手动清理它们。

Alternately, you can use the > command to indent, with ranges to go through the file somewhat efficiently manually. 或者,您可以使用>命令缩进,并使用范围手动有效地浏览文件。 99>k , for example, would indent the 99 lines below the cursor by one level. 例如, 99>k会将光标下方的99行缩进一级。

I've found the reindent script http://pypi.python.org/pypi/Reindent/0.1.0 works well for me. 我发现重新编写的脚本http://pypi.python.org/pypi/Reindent/0.1.0对我很有用。 Not pure vim, but really easy! 不纯粹的vim,但真的很容易!

After its installed you can use it in vim with 安装完成后,您可以在vim中使用它

:%! reindent

(ie pipe the entire buffer through the reindent program) and it's done. (即通过reindent程序管理整个缓冲区)并完成。

From the command line it can be used to reindent multiple files (eg all files in a directory, or even recursively down a directory tree). 从命令行,它可用于重新加载多个文件(例如,目录中的所有文件,甚至递归到目录树中)。

try the following substitution command: 尝试以下替换命令:

:%s/  /    /g

(To clarify: there are two spaces between the first and second '/' and four the second and third '/'.) (澄清一下:第一个和第二个'/'之间有两个空格,第二个和第三个'/'有四个空格。)

One helpful command when working with whitespace issues is also the set list command which will visually show all whitespace. 处理空白问题时,一个有用的命令也是set list命令,它将直观地显示所有空格。 Use set nolist to unset. 使用set nolist取消设置。

The best current way to reformat Python, fix many other issues, and also make it PEP8 compliant is to use autopep8 . 重新格式化Python,修复许多其他问题以及使其符合PEP8的最佳方法是使用autopep8 See this related question . 看到这个相关的问题 So after you've installed autopep8 (eg pip install autopep8 ) in vim you do: 所以在你在vim中安装autopep8(例如pip install autopep8 )之后你会:

:%! autopep8 -

There's also a vim-autopep8 plugin to make things even simpler. 还有一个vim-autopep8插件,可以让事情变得更简单。

The vim plugin vim-autoformat integrates the formatter autopep8 into vim automatically, if it is installed. vim插件vim- autoformat 自动将格式化程序autopep8集成到vim中(如果已安装)。 You can format the whole file, or the visually selected part using a single keystroke. 您可以使用单个按键格式化整个文件或可视选择的零件。

More importantly, vim-autoformat takes the relevant settings of your .vimrc into account, eg if you have 更重要的是,vim-autoformat会将.vimrc的相关设置考虑在内,例如,如果有的话

set shiftwidth=4

in your .vimrc , it will pass this information on to autopep8. .vimrc ,它会将此信息传递给autopep8。

Have you tried? 你有没有尝试过?

:retab :雷泰

I'm not in front of a machine with Vim at the moment so I can't verify this. 我现在不在Vim的机器前面所以我无法验证这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM