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\vim73I 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
.
精彩评论