Re: Help on search and replace
Dudley Fox wrote: Hello Vim List, I have used vim for a while, and though no expert I am fairly comfortable with the common commands. Recently I ran into a situation where I just couldn't find a way to do a search and replace. I was hoping some of you experts could help me out. Starting text: nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) nameTable[pattern with spaces1] = ("pattern with spaces1", 67890) nameTable[pattern with spaces2] = ("pattern with spaces2", 243) nameTable[pattern with spaces3] = ("pattern with spaces3", 421) nameTable[pattern with spaces4] = ("pattern with spaces4", 3455) nameTable[pattern with spaces5] = ("pattern with spaces5", ) Desired Text: nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) nameTable[patternwithspaces1] = ("pattern with spaces1", 67890) nameTable[patternwithspaces2] = ("pattern with spaces2", 243) nameTable[patternwithspaces3] = ("pattern with spaces3", 421) nameTable[patternwithspaces4] = ("pattern with spaces4", 3455) nameTable[patternwithspaces5] = ("pattern with spaces5", ) Notice that the only difference is that the spaces are removed from the pattern in between the square brackets. I think I want to use \zs and \ze, but I couldn't wrap my head around the syntax. Any help would be appreciated. In addition to the various regexp solutions you've been given: if what you want done is actually contiguous linewise as shown, then vis.vim makes it fairly straightforward (http://mysite.verizon.net/astronaut/vim/index.html#VIS or http://vim.sourceforge.net/scripts/script.php?script_id=1195): select pattern with spaces0 ... pattern with spaces5 using ctrl-v and motion. :B s/ //g Regards, Chip Campbell
Re: Help on search and replace
Dudley Fox wrote: > Starting text: > nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) > > Desired Text: > nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) Notwithstanding the usefulness of sub-replace-special, which I also discovered in this thread, I find zero-width look-ahead/behind assertions to be very powerful: :%s/\v(\[[^]]*)@<=\s//g ( \[ [^]]* )@<= \s I also happen to like \v, but that's just syntactic sugar! Tobia
Re: Help on search and replace
On 3/29/07, A.J.Mechelynck <[EMAIL PROTECTED]> wrote: Dudley Fox wrote: [...] > This is the expression which worked for me in case anyone else > want to do a similar search. > :%s/\[.\{-}\]/\=substitute(submatch(0),'\s','','g')/c > > Thanks again. > > Enjoy, > Dudley I see you caught my error where I put in \0 which should have been submatch(0) :-) Yeah. I used your search expression, and Tim's replace expression. Works like a charm. :) Enjoy, Dudley Best regards, Tony. -- hundred-and-one symptoms of being an internet addict: 148. You find it easier to dial-up the National Weather Service Weather/your_town/now.html than to simply look out the window.
Re: Help on search and replace
Dudley Fox wrote: [...] This is the expression which worked for me in case anyone else want to do a similar search. :%s/\[.\{-}\]/\=substitute(submatch(0),'\s','','g')/c Thanks again. Enjoy, Dudley I see you caught my error where I put in \0 which should have been submatch(0) :-) Best regards, Tony. -- hundred-and-one symptoms of being an internet addict: 148. You find it easier to dial-up the National Weather Service Weather/your_town/now.html than to simply look out the window.
Re: Help on search and replace
On 3/29/07, Dudley Fox <[EMAIL PROTECTED]> wrote: On 3/29/07, Tim Chase <[EMAIL PROTECTED]> wrote: > > I have used vim for a while, and though no expert I am fairly > > comfortable with the common commands. Recently I ran into a situation > > where I just couldn't find a way to do a search and replace. I was > > hoping some of you experts could help me out. > > > > Starting text: > > nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) > > nameTable[pattern with spaces1] = ("pattern with spaces1", 67890) > > nameTable[pattern with spaces2] = ("pattern with spaces2", 243) > > nameTable[pattern with spaces3] = ("pattern with spaces3", 421) > > nameTable[pattern with spaces4] = ("pattern with spaces4", 3455) > > nameTable[pattern with spaces5] = ("pattern with spaces5", ) > > > > Desired Text: > > nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) > > nameTable[patternwithspaces1] = ("pattern with spaces1", 67890) > > nameTable[patternwithspaces2] = ("pattern with spaces2", 243) > > nameTable[patternwithspaces3] = ("pattern with spaces3", 421) > > nameTable[patternwithspaces4] = ("pattern with spaces4", 3455) > > nameTable[patternwithspaces5] = ("pattern with spaces5", ) > > > > > > Notice that the only difference is that the spaces are removed from > > the pattern in between the square brackets. I think I want to use \zs > > and \ze, but I couldn't wrap my head around the syntax. Any help would > > be appreciated. > > There are a number of ways to do this depending on the complexity > of your document. For the case you describe, this could easily > just be done with > > :%s/pattern with spaces/patternwithspaces > > By omitting the "g" flag, it replaces only the first instance on > the line. > > If, however, "pattern with spacesN" each represents a different > pattern, things get a little more complex. They are indeed different patterns. >Something like the following might do the trick: > > :%s/nameTable\[\zs[^]]*/\=substitute(submatch(0), '\s', '', 'g') > > This, as you suggested, uses the \zs tag. However, it also uses > the incredibly-useful "\=" for expression evaluation which you > can read more about at > > :help sub-replace-special > > You could tighten that search pattern if you needed, so that it > became > > /nameTable\[\zs[^]]*\ze] = ("... I think this is what I am looking for. Thanks for pointing out the sub-replace-special. I didn't know that existed. > Or if you needed to make it really tight, you could do something > like (broken into multiple lines for clarity, but should be all > one line with no spaces in the joining) > > :%s/ > \(nameTable\[\) > \([^]]\+\) > \(] = ("\1", \d\+)\) > /\= > submatch(1). > substitute(submatch(2), '\s', '', 'g'). > submatch(3) > > This will only match lines where the pattern with spaces appears > in both places...the one you want to replace, and the 2nd half > that you don't want to change. Fortunately I don't need it that strict. > > All sorts of crazy stuff. That last one is the tightest to what > you describe, but you might be able to get away with one of the > lazier options above. :) I am all about lazy. Thanks for your help. Tonight when I get the chance I will try my new found vim knowledge. This will make life much easier for me. Well I couldn't really wait until I got home, so I tried it here at work. This is the expression which worked for me in case anyone else want to do a similar search. :%s/\[.\{-}\]/\=substitute(submatch(0),'\s','','g')/c Thanks again. Enjoy, Dudley > > -tim Thanks to everyone else who responded as well. Enjoy, Dudley
Re: Help on search and replace
On 3/29/07, Tim Chase <[EMAIL PROTECTED]> wrote: > I have used vim for a while, and though no expert I am fairly > comfortable with the common commands. Recently I ran into a situation > where I just couldn't find a way to do a search and replace. I was > hoping some of you experts could help me out. > > Starting text: > nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) > nameTable[pattern with spaces1] = ("pattern with spaces1", 67890) > nameTable[pattern with spaces2] = ("pattern with spaces2", 243) > nameTable[pattern with spaces3] = ("pattern with spaces3", 421) > nameTable[pattern with spaces4] = ("pattern with spaces4", 3455) > nameTable[pattern with spaces5] = ("pattern with spaces5", ) > > Desired Text: > nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) > nameTable[patternwithspaces1] = ("pattern with spaces1", 67890) > nameTable[patternwithspaces2] = ("pattern with spaces2", 243) > nameTable[patternwithspaces3] = ("pattern with spaces3", 421) > nameTable[patternwithspaces4] = ("pattern with spaces4", 3455) > nameTable[patternwithspaces5] = ("pattern with spaces5", ) > > > Notice that the only difference is that the spaces are removed from > the pattern in between the square brackets. I think I want to use \zs > and \ze, but I couldn't wrap my head around the syntax. Any help would > be appreciated. There are a number of ways to do this depending on the complexity of your document. For the case you describe, this could easily just be done with :%s/pattern with spaces/patternwithspaces By omitting the "g" flag, it replaces only the first instance on the line. If, however, "pattern with spacesN" each represents a different pattern, things get a little more complex. They are indeed different patterns. Something like the following might do the trick: :%s/nameTable\[\zs[^]]*/\=substitute(submatch(0), '\s', '', 'g') This, as you suggested, uses the \zs tag. However, it also uses the incredibly-useful "\=" for expression evaluation which you can read more about at :help sub-replace-special You could tighten that search pattern if you needed, so that it became /nameTable\[\zs[^]]*\ze] = ("... I think this is what I am looking for. Thanks for pointing out the sub-replace-special. I didn't know that existed. Or if you needed to make it really tight, you could do something like (broken into multiple lines for clarity, but should be all one line with no spaces in the joining) :%s/ \(nameTable\[\) \([^]]\+\) \(] = ("\1", \d\+)\) /\= submatch(1). substitute(submatch(2), '\s', '', 'g'). submatch(3) This will only match lines where the pattern with spaces appears in both places...the one you want to replace, and the 2nd half that you don't want to change. Fortunately I don't need it that strict. All sorts of crazy stuff. That last one is the tightest to what you describe, but you might be able to get away with one of the lazier options above. :) I am all about lazy. Thanks for your help. Tonight when I get the chance I will try my new found vim knowledge. This will make life much easier for me. -tim Thanks to everyone else who responded as well. Enjoy, Dudley
RE: Help on search and replace
In this example I think it´s easy: [from line],[to line]s/ // and applicated twice. It only deletes every time the first space. Dada --- Dudley Fox <[EMAIL PROTECTED]> schrieb: > Hello Vim List, > I have used vim for a while, and though no expert I > am fairly > comfortable with the common commands. Recently I ran > into a situation > where I just couldn't find a way to do a search and > replace. I was > hoping some of you experts could help me out. > > Starting text: > nameTable[pattern with spaces0] = ("pattern with > spaces0", 12345) > nameTable[pattern with spaces1] = ("pattern with > spaces1", 67890) > nameTable[pattern with spaces2] = ("pattern with > spaces2", 243) > nameTable[pattern with spaces3] = ("pattern with > spaces3", 421) > nameTable[pattern with spaces4] = ("pattern with > spaces4", 3455) > nameTable[pattern with spaces5] = ("pattern with > spaces5", ) > > Desired Text: > nameTable[patternwithspaces0] = ("pattern with > spaces0", 12345) > nameTable[patternwithspaces1] = ("pattern with > spaces1", 67890) > nameTable[patternwithspaces2] = ("pattern with > spaces2", 243) > nameTable[patternwithspaces3] = ("pattern with > spaces3", 421) > nameTable[patternwithspaces4] = ("pattern with > spaces4", 3455) > nameTable[patternwithspaces5] = ("pattern with > spaces5", ) > > > Notice that the only difference is that the spaces > are removed from > the pattern in between the square brackets. I think > I want to use \zs > and \ze, but I couldn't wrap my head around the > syntax. Any help would > be appreciated. > > Sincerely, > Dudley > . . . . . . . ___ Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
Re: Help on search and replace
Dudley Fox wrote: Hello Vim List, I have used vim for a while, and though no expert I am fairly comfortable with the common commands. Recently I ran into a situation where I just couldn't find a way to do a search and replace. I was hoping some of you experts could help me out. Starting text: nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) nameTable[pattern with spaces1] = ("pattern with spaces1", 67890) nameTable[pattern with spaces2] = ("pattern with spaces2", 243) nameTable[pattern with spaces3] = ("pattern with spaces3", 421) nameTable[pattern with spaces4] = ("pattern with spaces4", 3455) nameTable[pattern with spaces5] = ("pattern with spaces5", ) Desired Text: nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) nameTable[patternwithspaces1] = ("pattern with spaces1", 67890) nameTable[patternwithspaces2] = ("pattern with spaces2", 243) nameTable[patternwithspaces3] = ("pattern with spaces3", 421) nameTable[patternwithspaces4] = ("pattern with spaces4", 3455) nameTable[patternwithspaces5] = ("pattern with spaces5", ) Notice that the only difference is that the spaces are removed from the pattern in between the square brackets. I think I want to use \zs and \ze, but I couldn't wrap my head around the syntax. Any help would be appreciated. Sincerely, Dudley (untested) :%s/\[.{-}\]/\=substitute('\0','\s','','g') see :help sub-replace-expression :help substitute() Best regards, Tony. -- Infancy, n.: The period of our lives when, according to Wordsworth, "Heaven lies about us." The world begins lying about us pretty soon afterward. -- Ambrose Bierce
Re: Help on search and replace
> I have used vim for a while, and though no expert I am fairly > comfortable with the common commands. Recently I ran into a situation > where I just couldn't find a way to do a search and replace. I was > hoping some of you experts could help me out. > > Starting text: > nameTable[pattern with spaces0] = ("pattern with spaces0", 12345) > nameTable[pattern with spaces1] = ("pattern with spaces1", 67890) > nameTable[pattern with spaces2] = ("pattern with spaces2", 243) > nameTable[pattern with spaces3] = ("pattern with spaces3", 421) > nameTable[pattern with spaces4] = ("pattern with spaces4", 3455) > nameTable[pattern with spaces5] = ("pattern with spaces5", ) > > Desired Text: > nameTable[patternwithspaces0] = ("pattern with spaces0", 12345) > nameTable[patternwithspaces1] = ("pattern with spaces1", 67890) > nameTable[patternwithspaces2] = ("pattern with spaces2", 243) > nameTable[patternwithspaces3] = ("pattern with spaces3", 421) > nameTable[patternwithspaces4] = ("pattern with spaces4", 3455) > nameTable[patternwithspaces5] = ("pattern with spaces5", ) > > > Notice that the only difference is that the spaces are removed from > the pattern in between the square brackets. I think I want to use \zs > and \ze, but I couldn't wrap my head around the syntax. Any help would > be appreciated. There are a number of ways to do this depending on the complexity of your document. For the case you describe, this could easily just be done with :%s/pattern with spaces/patternwithspaces By omitting the "g" flag, it replaces only the first instance on the line. If, however, "pattern with spacesN" each represents a different pattern, things get a little more complex. Something like the following might do the trick: :%s/nameTable\[\zs[^]]*/\=substitute(submatch(0), '\s', '', 'g') This, as you suggested, uses the \zs tag. However, it also uses the incredibly-useful "\=" for expression evaluation which you can read more about at :help sub-replace-special You could tighten that search pattern if you needed, so that it became /nameTable\[\zs[^]]*\ze] = ("... Or if you needed to make it really tight, you could do something like (broken into multiple lines for clarity, but should be all one line with no spaces in the joining) :%s/ \(nameTable\[\) \([^]]\+\) \(] = ("\1", \d\+)\) /\= submatch(1). substitute(submatch(2), '\s', '', 'g'). submatch(3) This will only match lines where the pattern with spaces appears in both places...the one you want to replace, and the 2nd half that you don't want to change. All sorts of crazy stuff. That last one is the tightest to what you describe, but you might be able to get away with one of the lazier options above. :) -tim