在Vim中,有时需要将tab转换成space。使用ret命令(replace tab)。
[range]ret[ab]! [new-tabstop]
1.tab替换为空格
:set ts=4
:set expandtab
:%retab!
或三步简化为二步
:set expandtab
:%ret! 4
如果没有给定4,则用当前的tab宽度设定替换为space。
加!是用于处理非空白字符之后的TAB,即所有的TAB,若不加!,则只处理行首的TAB。
2.空格替换为TAB
:set ts=4
:set noexpandtab
:%retab!
其他方法:
1.使用sed命令
sed -i 's/\t/ /g' file_name
2.使用tr命令
cat filename | tr "\t" " " > filename_new
3.col -x命令
cat filename | col -x > filename_new
其它相关命令:
:set tabstop=4 设定tab宽度为4个字符
:set shiftwidth=4 设定自动缩进为4个字符
:set expandtab 用space替代tab的输入c
:set noexpandtab 不用space替代tab的输入