Friday, March 22, 2013

Crazy vim one line expression to search for something and replace it with a different search relative to the first match

vim search to find idnum=XXX& and replace the XXX part with a search from the line before that line of i=999:


:%s/\(idnum=\)\@<=[^&]\+/\=matchstr(getline(search('\(i=\)\@<=\d\+','b')),'\(i=\)\@<=\d\+')/

And it's much easier to explain that it was to figure out how to do it, or how it looks.

If anyone has an easier way, I'd love to hear it. I'm not very happy with the "getline" having to do the same search backwards, but I couldn't figure out the syntax for .-1 (by itself errored, ".-1", '.-1', "."-1 all failed).

2 comments:

  1. I'm sure you did a similar search, but did you find this one?

    http://stackoverflow.com/posts/834367/revisions

    ReplyDelete
  2. I did not, but that wouldn't do it for me since it expects the line before item to be constant in the columns I need, which I don't think is the case for my issue.

    ReplyDelete