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

53479RE: >> help with scripting vim / search & replace

Expand Messages
  • Dave Silvia
    Sep 14, 2004
    • 0 Attachment
      Try:

      function! SandR(old,new)
      let lnum=search(a:old)
      if !lnum
      return lnum
      endif
      normal dd
      normal k
      let newtext=a:new
      let NL=match(newtext,"\<NL>")
      while NL != -1
      let newline=strpart(newtext,0,NL)
      call append(line('.'),newline)
      normal j
      let newtext=strpart(newtext,NL+1)
      let NL=match(newtext,"\<NL>")
      endwhile
      if newtext != ''
      call append(line('.'),newtext)
      endif
      return lnum
      endfunction

      If the file in question has more than one occurrence of the line you want to
      replace, call it in a loop:

      while SandR("next comes the third line of text","This is some code\<NL>that
      needs to be inserted\<NL>with line breaks intact\<NL>\<NL>")
      endwhile

      thx,
      Dave S.

      *** -----Original Message-----
      *** From: admin@... [mailto:admin@...]
      *** Sent: Tuesday, September 14, 2004 12:46 AM
      *** To: vim@...
      *** Subject: Re: >> help with scripting vim / search & replace
      *** Importance: High
      ***
      ***
      *** 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.
      ***
      *** 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
      ***
      ***
      *** Is this a job for vim? Or can sed do the job?
      ***
      *** Regards,
      *** Devin
      ***
    • Show all 9 messages in this topic