Vim 仅在行首重新制表符空格
我当前正在使用
: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在与其他一些人讨论过这个问题后,我需要添加沉默!执行前的命令。
这就是我现在所做的工作:
所以现在这个函数将自动仅在每行开头用制表符替换空格。
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:
So now this function will automatically replace spaces with tabs at the beginning of each line only.
我使用不同的方法将 shell 脚本开头的空格更改为制表符。我只是从命令行使用 sed 。
使用 BSD sed:
*注意:(i) 方括号中的字符是制表符 (ii) /\1 后面的字符也是制表符。使用 Ctrl+v+Tab 组合键在终端中输入两个选项卡字符。
使用 GNU sed:
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:
*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: