使用Vim Retab解决TabError:缩进中的制表符和空格不一致?


问题内容

对于新手问题,我们深表歉意,但是我已经阅读了问题手册,并尝试了几次,但都没有得到预期的结果。

因此,我使用vim编辑文件(附加)。但是在运行时,出现了TabError:缩进错误中的制表符和空格不一致。

这是我尝试过的:

  • 用Vim打开文件。类型:retab:x。再次运行文件。仍然收到TabError消息。
  • 再次打开文件,然后输入:retab!:x。再次运行文件。仍然收到TabError消息。
  • 再次打开文件,然后输入:retab! 4:x。再次运行文件。这次可以用,但是我不知道为什么?另外,文件缩进似乎过长。(我在这里读到,编辑器可能在一个选项卡上显示8个空格)

我的问题是:

  • 这是什么:retab:retab!以及:retab! 4是什么意思?

  • 为什么:retab对我的文件不起作用?

    #!/usr/bin/env python
    

    Reduce function for computing matrix multiply A*B

    Input arguments:

    variable n should be set to the inner dimension of the matrix product (i.e., the number of columns of A/rows of B)

    import sys
    import string
    import numpy

    number of columns of A/rows of B

    n = int(sys.argv[1])

    Create data structures to hold the current row/column values (if needed; your code goes here)

    currentkey = None
    alist = [] # list for elelents in A
    blist = [] # list for elements in B

    input comes from STDIN (stream data that goes to the program)

    for line in sys.stdin:
    #Remove leading and trailing whitespace
    line = line.strip()
    #Get key/value
    key, value = line.split(‘\t’,1)
    print(key, value)
    #Parse key/value input (your code goes here)
    key = (key.split(‘,’, 1)[0], key.split(‘,’,1)[1])
    value = (value.split(‘,’, 1)[0], value.split(‘,’,1)[1], value.split(‘,’,1)[2])
    #If we are still on the same key…
    if key==currentkey:
    #Process key/value pair (your code goes here)
    # store all values in a lisl
    if value[0]==’A’:
    alist.append([value[1], value[2]])
    else:
    blist.append([value[1], value[2]])
    #Otherwise, if this is a new key…
    else:
    #If this is a new key and not the first key we’ve seen, i.e. currentkey!=None
    if currentkey:
    #compute/output result to STDOUT (your code goes here)
    alist = sorted(alist)
    blist = sorted(blist)
    newlist = [a[1]*b[1] for a,b in zip(alist, blist)]
    res = newlist.sum()
    print(currentkey, res)
    currentkey = key
    #Process input for new key (your code goes here)


问题答案:

只需输入:help retabVim并阅读。我认为我无法比帮助更好地解释它。也许您缺少可选范围部分;使用%前缀将其应用于整个文件。:set list向您展示每个字符也很有用;这将向您显示制表符和行尾(使用禁用:set nolist),:set <name>并且没有值可查看当前值,例如ex: set tabstop或后跟一些要设置的值。

通过示出的所有字符,启用和禁用的选项卡的扩展到空间与:set expandtab:set noexpandtab,设置制表位,并通过使用为前。:retab! 4您可以四处游玩,仅从制表符和空格之间切换,并更改制表符列的宽度。

这个链接,python的vim设置也可能有用