Vim 仅在行首重新制表符空格

发布于 2024-10-19 20:38:36 字数 708 浏览 1 评论 0原文

我当前正在使用

: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

处理函数 RetabIndents 时检测到错误:

第 2 行:

E486:未找到模式:^( {4})+

I'm currentling using

:set noet|retab!

But the problem I'm running into is it's replacing all instances of 4 spaces to tabs throughout the entire file.
I need vim to only replace instances of 4 spaces at the beginning of lines only.

If I remove the ! at the end of retab, spaces are not replaced anywhere.

I've tried using a custom function that somebody created:

" 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

but I get a nice little error message when I run:

:RetabIndents

Error detected while processing function RetabIndents:

line 2:

E486: Pattern not found: ^( {4})+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2024-10-26 20:38:36

在与其他一些人讨论过这个问题后,我需要添加沉默!执行前的命令。
这就是我现在所做的工作:

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

所以现在这个函数将自动仅在每行开头用制表符替换空格。

After talking with some other people about this, I needed to add the silent! command before execute.
So this is what I have working now:

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

So now this function will automatically replace spaces with tabs at the beginning of each line only.

热情消退 2024-10-26 20:38:36

我使用不同的方法将 shell 脚本开头的空格更改为制表符。我只是从命令行使用 sed 。

使用 BSD sed:

sed -i "" -e ':loop' -e "s/^\([ ]*\)  /\1   /" -e 't loop' somefile.sh

*注意:(i) 方括号中的字符是制表符 (ii) /\1 后面的字符也是制表符。使用 Ctrl+v+Tab 组合键在终端中输入两个选项卡字符。

使用 GNU sed:

sed -i -e ':loop' -e 's/^\([\t]*\)  /\1\t/' -e 't loop' somefile.sh

I use a different method to change spaces to tabs at beginning of my shell scripts. I just use sed from the command line.

Using BSD sed:

sed -i "" -e ':loop' -e "s/^\([ ]*\)  /\1   /" -e 't loop' somefile.sh

*note: (i) the character in the square brackets is a tab character (ii) the character aftger the /\1 is also a tab character. Both tabs cgharacters are entered in the terminal using Ctrl+v+Tab key combination.

Using GNU sed:

sed -i -e ':loop' -e 's/^\([\t]*\)  /\1\t/' -e 't loop' somefile.sh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文