我想将tab转换为gVim中的空格。 我_vimrc下行添加到我的_vimrc :
set tabstop=2
它可以在两个空格处停止,但它仍然看起来像是插入了一个tab键(我尝试使用h键来计算空格)。
我不知道如何将gVim转换为空格?
#1楼
本文有一个很好的vimrc脚本,用于处理选项卡+空格,并在它们之间进行转换。
提供以下命令:
Space2Tab仅在缩进中将空格转换为制表符。
Tab2Space仅将选项卡转换为空格。
RetabIndent执行Space2Tab(如果设置了'expandtab')或Tab2Space(否则)。
每个命令都接受一个参数,该参数指定选项卡列中的空格数。 默认情况下,使用'tabstop'设置。
" 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(,,0,)
command! -nargs=? -range=% Tab2Space call IndentConvert(,,1,)
command! -nargs=? -range=% RetabIndent call IndentConvert(,,&et,)
当我第一次寻找解决方案时,这对我的帮助有所帮助。
#2楼
首先在文件中搜索选项卡:/ ^ I:设置expandtab:retab
将工作。
#3楼
gg=G将重新整理整个文件,并删除大部分(如果不是全部)我从同事那里得到的文件。
#4楼
将以下行添加到.vimrc中
set expandtab
set tabstop=4
set shiftwidth=4
map :retab :wq!
在vim中打开文件并按F2键。选项卡将转换为4个空格,文件将自动保存。
#5楼
这对我有用:
你可以看到首先这样做的标签:
:set list
然后可以替换制表符然后执行以下操作:
:set expandtab
然后
:retab
现在所有选项卡都已替换为空格,然后您可以返回到正常查看,如下所示:
:set nolist