我想在 gVim 中将制表符转换为空格。我将以下行添加到我的_vimrc
:
set tabstop=2
它可以在两个空格处停止,但看起来仍然像插入了一个制表键(我后来尝试使用 h 键来计算空格)。
我不确定我应该怎么做才能让 gVim 将制表符转换为空格?
根据其他答案打开扩展选项卡后,根据新设置转换现有文件的极其方便的方法是:
:retab
它将在当前缓冲区上工作。
IIRC,类似于:
set tabstop=2 shiftwidth=2 expandtab
应该做的伎俩。如果您已经有制表符,那么请使用一个不错的全局 RE 将其替换为双空格。
如果您已经有要替换的标签,
:retab
尝试
set expandtab
对于软标签。
要修复预先存在的选项卡:
:%s/\t/ /g
我使用了两个空格,因为您已经将制表位设置为 2 个空格。
这对我有用:
你可以看到标签首先这样做:
:set list
然后为了可以替换标签然后执行以下操作:
:set expandtab
然后
:retab
现在所有选项卡都已替换为空格,然后您可以像这样返回正常查看:
:set nolist
gg=G
将重新缩进整个文件并删除我从同事那里获得的文件中的大部分(如果不是全部)选项卡。
将以下行添加到您的 .vimrc
set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>
在 vim 中打开文件并按 F2 选项卡将转换为 4 个空格并自动保存文件。
如果要保持\t
等于 8 个空格,请考虑设置:
set softtabstop=2 tabstop=8 shiftwidth=2
这将为您每次<TAB>
按下两个空格,但\t
您的代码中的实际仍将被视为 8 个字符。
This got it working for me:
:set tabstop=2 shiftwidth=2 expandtab | retab
本文有一个出色的 vimrc 脚本,用于处理制表符+空格,并在它们之间进行转换。
提供了这些命令:
Space2Tab 将空格转换为制表符,仅在缩进中。
Tab2Space 将制表符转换为空格,仅在缩进中。
RetabIndent 执行 Space2Tab(如果设置了 'expandtab')或 Tab2Space(否则)。
每个命令都接受一个参数,该参数指定制表符列中的空格数。默认情况下,使用“tabstop”设置。
来源:http: //vim.wikia.com/wiki/Super_retab#Script
" Return indent (all whitespace at start of a line), converted from
" tabs to spaces if what = 1, or from spaces to tabs otherwise.
" When converting to tabs, result has no redundant spaces.
function! Indenting(indent, what, cols)
let spccol = repeat(' ', a:cols)
let result = substitute(a:indent, spccol, '\t', 'g')
let result = substitute(result, ' \+\ze\t', '', 'g')
if a:what == 1
let result = substitute(result, '\t', spccol, 'g')
endif
return result
endfunction
" Convert whitespace used for indenting (before first non-whitespace).
" what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces).
" cols = string with number of columns per tab, or empty to use 'tabstop'.
" The cursor position is restored, but the cursor will be in a different
" column when the number of characters in the indent of the line is changed.
function! IndentConvert(line1, line2, what, cols)
let savepos = getpos('.')
let cols = empty(a:cols) ? &tabstop : a:cols
execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e'
call histdel('search', -1)
call setpos('.', savepos)
endfunction
command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>)
command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>)
command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>)
这对我的帮助比我第一次寻找解决方案时这里的答案要多。
首先在文件中搜索标签:/^I :set expandtab :retab
将工作。
expand
是一个将制表符转换为空格的 unix 实用程序。如果你不想set
在 vim 中做任何事情,你可以使用 vim 的 shell 命令:
:!% expand -t8
if u are using makefile or other text file, which need real tab other than some spaces,
add set noexpandtab
in your ~/vimrc first,
or just input set noexpandtab
command,
when edit some file with vi(vim)