Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for lines in a file that match a 2-dimensional pattern.

For instance, given the following input file:

aaaba
aabaa
aaaba
aaaaa
aaaaa

I'd like to use a pattern like

ab
ba
ab

to match on the top 3 lines. Ideally, I'd like to be able to make a substitution with that pattern, for instance, replacing it with

ba
ab
ba
share|improve this question

1 Answer 1

This cannot be done via regular expressions alone. You'd need a special atom that works like a capture group, but instead captures the column, and then a corresponding atom for recall. Best you can do is matching each row on a subsequent line, for your example

/ab\ze.*\n.*ba.*\n.*ab/

With that, not just lines that match the block's lines on their own are matched, but all occurrences that contain the block's text, though not just with the same vertical alignment, and only the first line's match is highlighted. My SearchHighlighting plugin provides this functionality (mapped to {Visual}*), too.

If that doesn't suffice (you can use the /c flag on :substitute to manually verify and acknowledge each match), you have to build your own search (and substitute) infrastructure, i.e. equivalents of /, n / N, and :s, and do the additional assertion for block-alignment in Vimscript.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.