How to batch process files in Vim and change tabs to spaces?
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 16:41:14
0
10
757

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梦

reply all(10)
大家讲道理

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.

習慣沉默

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

洪涛

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.

伊谢尔伦
:%s/\t/    /g
大家讲道理

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

黄舟

expand The shell command does this.

某草草

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

Peter_Zhu

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

给我你的怀抱
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.

我想大声告诉你

:h retab

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template