Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim matches a rectangle area

Tags:

regex

vim

I want to match a rectangle area in Vim using a regex expression, for example:

abcd test1
abcd test2

I want to match test1 and test2 at once, but not abcds. (test1 and test2 are constant, we don't need to consider [0-9], that's just an example)

I want to match every column-aligned test1 test2

This

test1
test2

the rectangle area may appear anywhere, I can't assume it is at "column 3" or something of that sort.

If they are not aligned, don't match it.

I tried \1\@<=test1\n\(.*\)\@<=test2 but no luck, because lookahead breaks a group. (from :help \\@<=)

Does anyone know how to do it with only vim-regex? Thanks.


Edit:

A complicated example may be this one:

aaaaaaaaa
b test1 b
c test2 c
ddddddddd

match only test1 and test2.

Usin two or more regex is acceptable (one for test1 and the other for test2?)


Edit2:

This is just for fun, I am just curious about how much vim can achieve, it's not a serious problem, it may be boring and meaningless for many people and that is fine with me, please don't be bothered, good night :)

like image 544
Cychih Avatar asked Jan 06 '15 16:01

Cychih


1 Answers

Simply searching for /test[0-9] will suffice. But I think the spirit of the question is really more about visual blocks. In visual mode you can use text objects for movement. So, in this case:

  1. Search for test1.
  2. Press Control-V (to turn on visual block mode)
  3. Press w to visually select the entire word.
  4. Press j to visually select the next word in the column below the first one. (use a range to extend this rectangular block, e.g. 10j would visually select the next ten items in that column.)
like image 132
gregory Avatar answered Nov 15 '22 09:11

gregory