Author Topic: next-find, what about replace?  (Read 1881 times)

jporkkahtc

  • Senior Community Member
  • Posts: 2296
  • Hero Points: 197
  • Text
next-find, what about replace?
« on: May 08, 2015, 06:13:05 pm »
I was trying to do an interesting RE search and replace.
I have a file like
HEADER:
 entry1
 entry2
ANOTHERHEADER:
 entry1
 entry2


I'd like to have slick transform this to prefix each "entry" line with the previous header.


I made an RE which changes the 1st two lines to
 HEADER, entry1
HEADER:



Find: \c(^[A-Z]+):\n(^ )(.*)$
Replace: \1, \3\n\1:


and used "/c" in the RE to place the cursor back to the first line.
But, I didn't outsmart SlickEdit: The next replace happened on the 2nd header -- so niether "entry2" got fixed.


So, whats a good way to do this?


I've got "next-find" bound to F3, but there doesn't seem to be a "next-replace"

Graeme

  • Senior Community Member
  • Posts: 2641
  • Hero Points: 330
Re: next-find, what about replace?
« Reply #1 on: May 09, 2015, 12:40:22 pm »
So you want
HEADER:
 entry1
 entry2

to become
HEADER: entry1
HEADER: entry2

is that correct?
Recording a macro would be easier than a regular expression.

JimmieC

  • Senior Community Member
  • Posts: 470
  • Hero Points: 16
Re: next-find, what about replace?
« Reply #2 on: May 12, 2015, 02:28:59 pm »
>> Recording a macro would be easier than a regular expression.

Agreed. I have seen some regular expression implementations that support spanning lines. But, it gets convoluted to work with. For just a few steps like this, a recorded macro is better.

You can record one iteration, then use Ctrl-F12 to repeat the iteration through the file.

However, if you have this need to perform this operation on files regularly, saving the macro won't work for you. You will have to record the macro every time. I use recorded macros and for me the typical recording is just what I explained, a single iteration, the use Ctrl-F12 to repeat. The saved macro will not have a step-repeat mechanism as Ctrl-F12 will not work for that. Ctrl-F12 is not really "Execute Last Macro" it is "Execute Last 'Recorded' Macro" so just be aware of this.

Jim

jporkkahtc

  • Senior Community Member
  • Posts: 2296
  • Hero Points: 197
  • Text
Re: next-find, what about replace?
« Reply #3 on: May 12, 2015, 02:42:39 pm »
I figured I would need a macro.

The RE here does work, except for the repetition - Slick naturally wants to avoid getting into a never-ending search and replace, so it avoids processing the same bit of text more than once. (I think that after a replace the next match attempt always starts at the character past the previously matched/replaced block of text).

Dennis

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 3401
  • Hero Points: 465
Re: next-find, what about replace?
« Reply #4 on: May 12, 2015, 10:09:13 pm »
On recording macros.  You might want to check out the commands "execute_last_macro_key" and "record_macro-end-execute-key".  I keep Alt+F1 through Alt+F12 bound to "record-macro-end-execute-key" so that I can keep an array of 12 recently recorded macros.  The only tricky part is remembering what each one does.