search

Home  >  Q&A  >  body text

How to batch process files in Vim and change tabs to spaces?

My coding habits were not very good in the past, so a lot of tabs were used in the code.
Now I want to convert tabs to spaces by setting vimrc and then executing the :ret command. The problem is that there are many files that need to be processed. Manually execute # for each file. ##:retThe command is not very practical. From what angle can I consider to solve this problem?

过去多啦不再A梦过去多啦不再A梦2776 days ago774

reply all(10)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 16:43:14

    For example, all you want to process are python files, then cd to the project root directory:
    1. $> vim `find . -type f -name "*.py"` // Open all files to be processed with VIM
    2. :argdo :ret | update // Execute the :ret command on all files in the vim parameter list and save

    In addition, you can learn more about the usage of bufdo and windo commands.

    The above is just to show that VIM can easily do this in response to the original poster's problem, without having to do it one by one. Of course, writing a script according to the idea above is also a good method.

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 16:43:14

    Try sed:sed -i -e "s/t/ /g" *.py

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:43:14

    It is recommended not to use vim and write a script to handle it.
    It can be written in shell, and the find + sed command will do it. It can also be written in python, and os.path.walk traverses the file and scans line by line. There is not much code.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 16:43:14

    :%s/\t/    /g
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 16:43:14

    This kind of problem can be solved using regular expressions, but it is best not to use vim to deal with it one by one. It would be better to write a python script to run regular expressions

    reply
    0
  • 黄舟

    黄舟2017-05-16 16:43:14

    expand The shell command does this.

    reply
    0
  • 某草草

    某草草2017-05-16 16:43:14

    It is more comfortable to use regular expressions for batch processing, but if you must use vim, @shizhz’s answer is good.

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 16:43:14

    sed -i "s/\t/ /g" *.py

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 16:43:14

    ls *.py | while read file;do
        vi $file<<EOF
    :%s/^I/    /g
    :wq
    EOF
    done
    

    Use the non-interactive mode of vi(m).
    ^I使用CTRL+V+IEnter.
    No spaces can be used after EOF.
    There cannot be a space before the vi(m) command.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-16 16:43:14

    :h retab

    reply
    0
  • Cancelreply