在(n)个字符后插入一个字符并重复(n)次-Vim [英] Insert a character after (n) characters and repeat(n) times - vim

查看:83
本文介绍了在(n)个字符后插入一个字符并重复(n)次-Vim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常会收到我们支持的票证,以便在isc-dhcp上进行dhcp预订.一些打印机(cof,cof HP)以此方式在测试页上打印mac地址信息:002481F33A9C;很明显,支持人员将这种方式写给了工单.我想更改为00:24:81:F3:3A:9C布局,并使用一种有效的方式进行插入:2个字符后4次".

Frequently i receive tickets from our support to make a dhcp reservation on isc-dhcp. Some printers(cof,cof HP) print the mac address information on the test page this way: 002481F33A9C ; and obviously the support guys write this way to the Ticket. I want to change to 00:24:81:F3:3A:9C layout and use an efficient way to do a "insert : after 2 characters 4 times".

我知道我可以使用宏来实现:

I know that i can achieve this with macros:

qq     " start recording -> register q
ll     " move the cursor 2 characters right
i      " enter insert mode
:      " insert :
<Esc>  " switch to normal mode and cursor goes 1 character back
l      " move the cursor 1 character right(since insert mode back one character)
q      " stop recording
4@q    " execute macro 4 times

然后这个:

host foo {
    hardware ethernet 002481F33A9C;
    fixed-address 192.x.x.x;
}

将变成这个:

host foo {
    hardware ethernet 00:24:81:F3:3A:9C;
    fixed-address 192.x.x.x;
}

是否有一种方法可以通过命令行方式以更快的方式进行操作而无需记录宏,因为我们有很多办公室,并且将带有此宏的vimrc传递给所有机器(和冗余机器)可能是痛苦吗?

Is there a way to make this on a faster way through the command-line mode and without recording a macro, because we have lots of offices, and passing a vimrc with this macro to all machines(and redundancy ones) could be a pain?

欢呼

推荐答案

可能的解决方案:

:%s/\v(hardware ethernet) (..)(..)(..)(..)(..)(..);$/\1 \2:\3:\4:\5:\6\:\7;/g

说明:

  • 打开非常魔术"模式

  • turn on 'very magic' mode

匹配字符串 hardware ethernet ,后跟12个字符,然后是分号和行尾

match the string hardware ethernet followed by 12 characters, then semicolon and end-of-line

将12个字符分为6个子表达式,每个子表达式由2个字符组成

divide the 12 characters into 6 sub-expressions, each made of 2 characters

替换在每个子表达式之间插入冒号的字符串

replace the string inserting a colon between each sub-expression

这篇关于在(n)个字符后插入一个字符并重复(n)次-Vim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆