使用Vim Retab解决TabError: inconsistent use of tabs and spaces in indentation?

新手上路,请多包涵

为新手问题道歉,但是我已经阅读了手册, 这个 问题,并且尝试了几次都没有我预期的结果。

所以我正在使用 vim 来编辑一个文件(附件)。但是在运行时,我得到了 TabError: inconsistent use of tabs and spaces in indentation 错误。

这是我尝试过的:

  • 用 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)

原文由 yuqli 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

只需在 Vim 中键入 :help retab 并阅读。我想我无法比帮助更好地解释它。也许您缺少可选范围部分;使用 % 前缀应用于整个文件。同样有用的是 :set list 向您展示每个字符;这将向您显示制表符和行尾(使用 :set nolist 禁用)和 :set <name> 没有值以查看当前值,例如 : set tabstop 设置。

通过显示所有字符,使用 :set expandtab:set noexpandtab 启用和禁用制表符扩展到空格,设置 tabstop 并使用 for ex。 :retab! 4 你可以玩自己的方式并从仅制表符和仅空格切换,并更改制表符列的宽度。

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

原文由 progmatico 发布,翻译遵循 CC BY-SA 3.0 许可协议

退出

:%s/\t/    /g

意思是用4个空格替换制表符。按 Enter Esc

 :wq

原文由 DARK_C0D3R 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题