Loading ...
Sorry, an error occurred while loading the content.

Re: Variable values in substitution

Expand Messages
  • Paul Y. Peng
    ... Thanks for these solutions. They work for me. I actually thought about the second solution, but I didn t know how to use more than one command in :global.
    Message 1 of 4 , Jan 1, 2001
    • 0 Attachment
      Benji Fisher wrote:
      >
      > "Paul Y. Peng" wrote:
      > >
      > > Happy New Millennium to All Vim Users!
      > >
      > > Is there any simple way to replace a string in a file by a new
      > > string including "n" that stands for the number of occurrence
      > > of the old string from the beginning of the file? For example,
      > > if a file contains a single line:
      > >
      > > abcrtmabc ntmabcabc
      > >
      > > I want replace "abc" by "abn" so that the line becomes
      > >
      > > ab1rtmab2 ntmab3ab4
      > >
      > > If this is possible, could I use any range of numbers rather than
      > > 1-(#occurence of the string) for "n"? Any clues are appreciated.
      > > I use Vim5.6 on Win98.
      > >
      > > Paul.
      >
      > I am not sure I would call this "simple," but try these lines in a
      > vim function:
      >
      > " dummy line so search will not fail
      > $put='abc'
      > let n = 0
      > 0/abc
      > while line(".") < line("$")
      > let n = n+1
      > execute "substitute/abc/ab" . n
      > if getline(".") !~ "abc"
      > /abc
      > endif
      > endwhile
      > " Now, delete the dummy line:
      > $delete
      >
      > Notes: if you do not mind searching from the top each time, "0/abc" is
      > simpler than the conditional search in the loop. When you break out of
      > the loop, you should be on the last line, so you can omit the "$" in
      > "$delete" if you think that is better style. (I think that the version
      > above is clearer than this alternative.) In vim 6.0, you can use the
      > search() function instead of the searches, and avoid the dummy line. If
      > you are not worried about multiple copies of your pattern on one line, you
      > can do
      >
      > :let n = 0
      > :g/abc/let n = n+1 | execute "s/abc/ab" . n

      Thanks for these solutions. They work for me. I actually thought
      about the second solution, but I didn't know how to use more than
      one command in :global. Even now, if I type :help |, I got a help
      page about "bar", which doesn't seem to be relevant to the use of
      "|" above:(

      Thank you Benji,
      Paul.
    • Benji Fisher
      ... [snip] ... You are welcome. After typing :help | , instead of hitting , try (unless you have some other value set for wildchar ). The second
      Message 2 of 4 , Jan 1, 2001
      • 0 Attachment
        "Paul Y. Peng" wrote:
        >
        > Benji Fisher wrote:
        > >
        [snip]
        > >
        > > :let n = 0
        > > :g/abc/let n = n+1 | execute "s/abc/ab" . n
        >
        > Thanks for these solutions. They work for me. I actually thought
        > about the second solution, but I didn't know how to use more than
        > one command in :global. Even now, if I type :help |, I got a help
        > page about "bar", which doesn't seem to be relevant to the use of
        > "|" above:(
        >
        > Thank you Benji,
        > Paul.

        You are welcome. After typing ":help |", instead of hitting <CR>,
        try <Tab> (unless you have some other value set for 'wildchar'). The
        second option listed is ":bar", and :help :bar takes you to the right
        page.

        --Benji Fisher
      Your message has been successfully submitted and would be delivered to recipients shortly.