设置vim缩进和TAB
在程序开发中,设置默认缩进4个空格,并且将TAB
替换成空格,比较符合常用的编程规范。
vim
的配置文件~/.vimrc
添加如下内容
set tabstop=4 " The width of a TAB is set to 4.
" Still it is a \t. It is just that
" Vim will interpret it to be having
" a width of 4.
set shiftwidth=4 " Indents will have a width of 4
set softtabstop=4 " Sets the number of columns for a TAB
set expandtab " Expand TABs to spaces
显示编辑文件中的TAB
在编辑文档时,文档中包含tab往往无法分辨,在vim中可以定义显示特殊字符的方法:
set list
set listchars=tab:>-
此时,如果文档中有tab符号,则会显示成>---
,便于检查。
空格替换为tab
vim提供了一个 !retab
命令来替换空格到 <tab>
# first in .vimrc set up
:set expandtab
:set tabstop=4
# or you can do this
:set tabstop=4 shiftwidth=4 expandtab
# then in the py file in command mode, run
:retab!
你也可以用命令
sed -e 's/ /\t/g' test.py > test.new.py
tab替换为空格
vim命令如下:
:set et|retab
你也可以用搜索来替换:
:%s/\t/ /g
参考
Last updated
Was this helpful?