开发者

VIM: cycle through string

I'm trying to change in an url all text longer then 6 characters to 6characters~1 p.e.

c:\program files\vim directory\vim73\ to

c:\progra~1\vimdir~1\vim73

I have found a way to find the string length betwee开发者_StackOverflown "\ \"

strlen(matchstr("c:\program files\vim directory\vim73\","\\\zs.*\ze\\"))

but how can I cycle through a string?

How can I cut it to 6 characters (removing the spaces if there are) and put "~1" behind it?


This seems to work for your example text but I'm not sure it would work in all cases (assumes your filename is on it's own line, you could modify it to be more specific otherwise):

:s# ##ge|s#\v\\\zs(\w{6}).{-}\ze\\#\1\~1#&

Edit: just noticed that this leaves a trailing slash. If you want that removed too you can use the following instead:

:s# ##ge|s#\v\\\zs(\w{6}).{-}\ze\\#\1\~1#&|s#\\$##&


I think this regex should do the trick:

:%s /\\\([^\\]\{6\}\)[^\\]*/\\\1\~1\\/g

It essentially means: Match the first 6 characters between two occurences of \ or one ocurrence of \ and newline. Remember those characters (\( ... \) is used to remember) and match everything else until the separator (newline or \). Substitute that with the first 6 characters and ~1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜