我想在 gVim 中将制表符转换为空格。我在 _vimrc
中添加了以下行:
set tabstop=2
它可以在两个空格处停止,但看起来仍然像插入了一个制表键(之后我尝试使用 h 键来计算空格)。
我不确定我应该怎么做才能让 gVim 将制表符转换为空格?
^I
来自 Ctrl-v
然后 Tab
)会起作用,例如,如果您当前的制表符是 4 个空格::%s/ /^I/g
根据其他答案打开扩展选项卡后,根据新设置转换现有文件的极其方便的方法是:
:retab
它将在当前缓冲区上工作。
IIRC,类似于:
set tabstop=2 shiftwidth=2 expandtab
应该做的伎俩。如果您已经有制表符,那么请使用一个不错的全局 RE 将其替换为双空格。
如果您已经有要替换的标签,
:retab
尝试
set expandtab
对于软标签。
要修复预先存在的选项卡:
:%s/\t/ /g
我使用了两个空格,因为您已经将制表位设置为 2 个空格。
set ts = 4
中使用的那样。
这对我有用:
你可以看到标签首先这样做:
:set list
然后为了可以替换标签然后执行以下操作:
:set expandtab
然后
:retab
现在所有选项卡都已替换为空格,然后您可以像这样返回正常查看:
:set nolist
:set list
很漂亮。
gg=G
将重新缩进整个文件并删除我从同事那里获得的文件中的大部分(如果不是全部)选项卡。
gg
将光标移动到缓冲区的开头。 =
是格式,接受一个移动命令。 G
将光标移动到缓冲区的末尾,因此它告诉 vim 从当前缓冲区的开头到结尾进行格式化。
将以下行添加到您的 .vimrc
set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>
在 vim 中打开一个文件并按 F2 选项卡将转换为 4 个空格并自动保存文件。
q
也会关闭文件的当前缓冲区。这可能是也可能不是所希望的。
如果您想让 \t
等于 8 个空格,请考虑设置:
set softtabstop=2 tabstop=8 shiftwidth=2
这将为您每按一次 <TAB>
提供两个空格,但您的代码中的实际 \t
仍将被视为 8 个字符。
softtabstop
,我认为这是杀手级功能之一。
这让它对我有用:
: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
:
。现在 vim 命令行显示 :'<,'>
。然后为制表符输入 !expand -t4
到 4 个空格。 (生成的命令行类似于 :'<,'>!expand -t4
)
如果你正在使用 makefile 或其他文本文件,除了一些空格之外还需要真正的制表符,请先在 ~/vimrc 中添加 set noexpandtab
,或者在使用 vi(vim) 编辑某些文件时输入 set noexpandtab
命令
不定期副业成功案例分享
:%s/\t/ /g
多年。:args retab | w
,例如vim *.txt
。