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

53498Re: >> help with scripting vim / search & replace

Expand Messages
  • Gary Johnson
    Sep 14, 2004
      On 2004-09-13, admin@... wrote:
      > Hello list,
      >
      > a while back I learned how to use sed and perl to replace certain patterns
      > in a text file thanks to some great suggestions from those on this list.
      >
      > Now, I need to do something very similar but I can't figure out how to
      > search
      > and/or replace with larger blocks of text with full lines and line breaks.

      To search for a line break, use \n in the pattern. To insert or
      replace with a line break, use \r in the replacement string. See

      :help sub-replace-special

      >
      > for example, take this text file:
      >
      > a simple line of text
      > and the second line of text is here
      > next comes the third line of text
      > and finally the fourth line of text
      >
      > to search for:
      >
      > and the second line of text is here
      > next comes the third line of text
      >
      > and replace it with:
      >
      > and the second line of text is here
      > This is some code
      > that needs to be inserted
      > with line breaks intact
      > <blank line>
      >
      > to end up with:
      >
      > a simple line of text
      > and the second line of text is here
      > This is some code
      > that needs to be inserted
      > with line breaks intact
      >
      > and finally the fourth line of text

      While this is kind of long (it's all on one line), this will do it:

      s/\(and the second line of text is here\)\nnext comes the third line of text/\1\rThis is some code\rthat needs to be inserted\rwith line breaks intact\r/

      See

      :help /\(

      for the use of \(...\) and \1.

      > Is this a job for vim? Or can sed do the job?

      You could probably use sed, but I wouldn't want to. It's language
      is very different from any other text processing language, and
      somewhat limited, so I don't use it often.

      HTH,
      Gary

      --
      Gary Johnson | Agilent Technologies
      garyjohn@... | Wireless Division
      | Spokane, Washington, USA
    • Show all 9 messages in this topic