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

Re: Multiple file find and replace

Expand Messages
  • Antoine J. Mechelynck
    ... In general, the format is ... or ... Note: :s is short for :substitute . represent word boundaries. The g flag means: everywhere, not only at
    Message 1 of 9 , Dec 2, 2004
    • 0 Attachment
      On 2/12/2004 11:30, Adam Cripps wrote:
      > I have a friend who uses Textpad and likes the multiple file search
      > and replace. I want to demonstrate this in Vim, but am unsure how to
      > do this.
      >
      > Any pointers?
      > TIA
      > Adam

      :see ":help :s"

      In general, the format is

      :s/<pattern>/<replacement text>/flags
      or
      :{range}s/<pattern>/replacement text>/flags

      For instance, to replace everywhere in a file "teh" (as a word) by "the":

      :1,$s/\<teh\>/the/g

      Note: ":s" is short for ":substitute". \< and \> represent word
      boundaries. The g flag means: everywhere, not only at most once per line.

      See ":help :s" (and scroll down) for more examples.

      HTH,
      Tony.
    • Tim Chase
      ... I think the OP was looking for how to do it across multiple files. While Jurgen suggested the argdo , it s also helpful to mention windo and bufdo
      Message 2 of 9 , Dec 2, 2004
      • 0 Attachment
        >> I have a friend who uses Textpad and likes the multiple file
        >> search and replace. I want to demonstrate this in Vim, but
        >> am unsure how to do this.
        >
        > :see ":help :s"

        I think the OP was looking for how to do it across multiple
        files. While Jurgen suggested the "argdo", it's also helpful to
        mention "windo" and "bufdo" which are also helpful.

        I'll give the caveat that with bufdo and argdo, you leave that
        buffer so you'll get an error if the file has been modified.
        Thus, you can either ":set hidden" beforehand (and then do a
        ":wall" to write all the changed files), or you can append "|w"
        to the ex command.

        So if you have started vim with "vim *.cpp", you can do

        :set hidden
        :argdo %s/foo/bar/ge

        (which is my preferred method) or, IIRC, you can do

        :argdo %s/foo/bar/ge |w

        to make the changes in each file and then write them before
        proceeding on to the next file.

        So an expanded combination of Jurgen's and Antoine's answers
        might yield pointers to

        :he windo
        :he argdo
        :he bufdo
        :he :s
        :he 'hidden'
        :he :s_flags

        (that last one is for the "e" in the :s command which supresses
        error reporting if no changes were made)

        Hope this helps,

        -tim
      • Antoine J. Mechelynck
        ... Yeah, sorry, I skipped the word file when reading the OP s text. Best regards, Tony.
        Message 3 of 9 , Dec 2, 2004
        • 0 Attachment
          On 2/12/2004 14:50, Tim Chase wrote:
          >>> I have a friend who uses Textpad and likes the multiple file
          >>> search and replace. I want to demonstrate this in Vim, but
          >>> am unsure how to do this.
          >>
          >>
          >> :see ":help :s"
          >
          >
          > I think the OP was looking for how to do it across multiple files. [...]

          Yeah, sorry, I skipped the word "file" when reading the OP's text.

          Best regards,
          Tony.
        • Steve Hall
          ... Cream has a vimscript that will find files matched with a wildcard, open each in turn, perform the substitution and close it again:
          Message 4 of 9 , Dec 2, 2004
          • 0 Attachment
            On Thu, 2004-12-02 at 10:30 +0000, Adam Cripps wrote:
            > I have a friend who uses Textpad and likes the multiple file search
            > and replace. I want to demonstrate this in Vim, but am unsure how to
            > do this.

            Cream has a vimscript that will find files matched with a wildcard,
            open each in turn, perform the substitution and close it again:

            http://cvs.sourceforge.net/viewcvs.py/cream/cream/cream-
            replacemulti.vim?view=markup

            Doing all this via script is slow (1/2 second per file), the dialog is
            worse than hackish. and it depends on a number of external Cream
            functions. But it is cross-platform (Windows and GNU/Linux). If a real
            grep ever finds it's way into Vim, this could all be sped up by
            limiting the files opened to those pre-determined to contain the match
            in the first place, too.

            However, sed -i is currently a better friend. ;)

            HTH.

            --
            Steve Hall [ digitect mindspring com ]
          • Wind
            Hello Steve, Do you know about :grep command? Friday, December 3, 2004, 5:49:26 AM, you wrote: ... SH Cream has a vimscript that will find files matched with
            Message 5 of 9 , Dec 3, 2004
            • 0 Attachment
              Hello Steve,
              Do you know about :grep command?
              Friday, December 3, 2004, 5:49:26 AM, you wrote:

              SH> On Thu, 2004-12-02 at 10:30 +0000, Adam Cripps wrote:
              >> I have a friend who uses Textpad and likes the multiple file search
              >> and replace. I want to demonstrate this in Vim, but am unsure how to
              >> do this.

              SH> Cream has a vimscript that will find files matched with a wildcard,
              SH> open each in turn, perform the substitution and close it again:

              SH> http://cvs.sourceforge.net/viewcvs.py/cream/cream/cream-
              SH> replacemulti.vim?view=markup

              SH> Doing all this via script is slow (1/2 second per file), the dialog is
              SH> worse than hackish. and it depends on a number of external Cream
              SH> functions. But it is cross-platform (Windows and GNU/Linux). If a real
              SH> grep ever finds it's way into Vim, this could all be sped up by
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
              SH> limiting the files opened to those pre-determined to contain the match
              SH> in the first place, too.

              SH> However, sed -i is currently a better friend. ;)

              SH> HTH.




              --
              Best regards,
              Wind mailto:sonic_wind@...
            • Steve Hall
              From: Wind, Dec 3, 2004 5:46 AM ... Yes, unfortunately it is only an interface to an external grep program--most Windows users do not have one available. --
              Message 6 of 9 , Dec 4, 2004
              • 0 Attachment
                From: Wind, Dec 3, 2004 5:46 AM
                >
                > Hello Steve,
                > Do you know about :grep command?

                Yes, unfortunately it is only an interface to an external grep
                program--most Windows users do not have one available.


                --
                Steve Hall [ digitect mindspring com ]
              • Alan G Isaac
                ... Actually I think most Windows users nowadays have findstr available as a grep utility, and if they do not like that they have the Unxutils grep as a
                Message 7 of 9 , Dec 4, 2004
                • 0 Attachment
                  > From: Wind, Dec 3, 2004 5:46 AM
                  >> Do you know about :grep command?

                  On Sat, 04 Dec 2004, Steve Hall apparently wrote:
                  > Yes, unfortunately it is only an interface to an external grep
                  > program--most Windows users do not have one available.

                  Actually I think most Windows users nowadays have 'findstr'
                  available as a grep utility, and if they do not like that
                  they have the Unxutils 'grep' as a free download.

                  fwiw,
                  Alan Isaac
                Your message has been successfully submitted and would be delivered to recipients shortly.