重新格式化 Python 文件以具有 4 个空格缩进 [英] Reformat a Python file to have 4 space indentations

查看:32
本文介绍了重新格式化 Python 文件以具有 4 个空格缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑一个 Python 文件,该文件使用两个空格作为程序缩进 - 我更喜欢 4 个空格.在我的 .vimrc 中,我有以下与缩进相关的设置:

I'm editing a Python file that uses two spaces for programmatic indents - I prefer 4 spaces. In my .vimrc I have the following settings related to indentation:

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

如何让 Vim 将所有现有的 2 个空格缩进转换为 4 个空格缩进?

How do I get Vim to convert all the existing 2 space indents to be 4 space indents?

换句话说:

if something:
  dothis()

变成

if something:
    dothis()

当我尝试 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

变成了

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.

这篇关于重新格式化 Python 文件以具有 4 个空格缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆