Vim 仅在行首重新制表空格

标签 vim tabs whitespace

我正在使用

: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/

相关文章:

delphi - 是否可以控制 JvDockServer 组件中选项卡的显示方式?

r - 如何在 RSTudio 中为 Flex 仪表板选项卡的标签选择颜色?

Vim - 跟踪准确的空白计数

intellij-idea - 如何默认显示隐藏字符(零宽度空间,即&#8203)

c++ - 如何为其他目录中的所有 C++ 文件生成标签文件?

perl - VIM 9 Perl 文件的字边界更改

vim 无法使用 fortran90 代码识别超过 72 的列

Vim 将高亮信息屏幕保存到文件

javascript - HTML+JavaScript GUI 建议

c# - 从 XML 元素中读取空格字符