我正在使用
:set noet|retab!
但是我遇到的问题是它正在将整个文件中 4 个空格的所有实例替换为制表符。
我需要 vim 仅替换行首的 4 个空格的实例。
如果我删除 !在 retab 结束时,任何地方都不会替换空格。
我尝试使用某人创建的自定义函数:
" Retab spaced file, but only indentation
command! RetabIndents call RetabIndents()
" Retab spaced file, but only indentation
func! RetabIndents()
let saved_view = winsaveview()
execute '%s@^\( \{'.&ts.'}\)\+@\=repeat("\t", len(submatch(0))/'.&ts.')@'
call winrestview(saved_view)
endfunc
但是当我运行时,我收到一条不错的小错误消息:
:RetabIndents
Error detected while processing function RetabIndents:
line 2:
E486: Pattern not found: ^( {4})+
最佳答案
和其他人谈过这件事后,我需要添加沉默!执行前命令。
所以这就是我现在的工作:
autocmd BufWritePre * :RetabIndents
command! RetabIndents call RetabIndents()
func! RetabIndents()
let saved_view = winsaveview()
execute '%s@^\(\ \{'.&ts.'\}\)\+@\=repeat("\t", len(submatch(0))/'.&ts.')@e'
call winrestview(saved_view)
endfunc
所以现在这个函数只会在每行的开头自动用制表符替换空格。
关于Vim 仅在行首重新制表空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5172137/