题
我想将tab转换为gVim中的空格。我将以下行添加到_vimrc
:
set tabstop=2
它可以在两个空格处停止,但它仍然看起来像是插入了一个Tab键(我之后尝试使用h键来计算空格)。
我不知道如何将gVim转换为空格?
解决方案
set tabstop=2 shiftwidth=2 expandtab
应该做的伎俩。如果你已经有了标签,那么请用一个漂亮的全局RE跟进它,用双倍空格替换它们。
其他提示
根据其他答案启用expandtab后,根据新设置转换现有文件的极为方便的方法是:
:retab
它将在当前缓冲区上工作。
尝试
set expandtab
用于软标签。
修复预先存在的标签:
:%s/\t/ /g
我使用了两个空格,因为你已经将tabstop设置为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>
按钮会给你两个空格,但代码中的实际<=>仍将被视为8个字符。
首先搜索文件中的标签:/ ^ I :设置expandtab :雷泰公司
会奏效。
expand
是一个将标签转换为空格的unix实用程序。如果您不想在vim中set
任何内容,可以使用vim中的shell命令:
:!% expand -t8
这让它对我有用:
: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>)
这比我第一次寻找解决方案时的答案更能帮助我。