在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的输入
本文介绍了在Vim编辑器中如何将tab转换为space以及反之的操作,包括使用%retab!命令和设置tabstop与expandtab选项。此外,还提到了使用sed、tr及col-x等外部命令进行转换的方法,并提供了相关配置选项如settabstop和setshiftwidth的用途。同时,讲解了!参数在retab命令中的作用,区分处理行首与非空白字符后的TAB。

5814

被折叠的 条评论
为什么被折叠?



