0

Vim has the very good feature to limit the range for searching to start at from-line up to end searching at end-line, e.g.:

/\%>1114l\%<1116lPATTERN

I would like to set the cursor to line 1115 and write instead

/\%>.-1l\%<.+1lPATTERN

Is this somehow possible?

Or can variables be used instead of typing numbers, and ahead of this the variables be evaluated to .-1 and .+1.

3
  • Hi Anton, maybe could you write an answer to your own question and accept it (you'll to wait 2 days). It will be good for the others to learn this nice feature ;-) Mar 4, 2023 at 12:06
  • 1
    OP is confused about those atoms so there is no nice feature to learn, here.
    – romainl
    Mar 4, 2023 at 13:16
  • Answers do not belong in Questions. I've removed it. If you have an answer (even from a link), it should be written as an Answer down below (with quotes and citations as necessary to make it possible to read the full answer without navigating to external sites to prevent link rot).
    – D. Ben Knoble
    Mar 4, 2023 at 13:52

2 Answers 2

0

Answers to my above question you can read in

https://github.com/vim/vim/issues/11051

There I erroniously wrote about this item as a vim issue. Thus this was closed. But the text exists, and you can read it.

The very good answers there and my comments there after testing result in a solution to above question.

Essential points are:

In vim limiting the range of "/"-search can be done in two ways:

1.

by using real line numbers "1112", "1118", e.g.:

/\%>1112l\%<11168lPATTERN

numbers possibly followed by "l" or "c".

2.

or by using single-character marks, " 'm", " 'n", this marks must not be followed by "l" or "c", that was the essential.

In folliwing epxample the cursor I set to line 1115.

In three lines before the cursor line and in three lines (cursor line plus up to two lines after it) the matches will be highlighted.

And the cursor will be moved to the first match of PATTERN after cursor position.

By default highlighting skips the first character of first line of range.

And by default the character after the corsor position is the first possible match (not where the cursor is).

The following code shows for debugging purposes the line numbers of first line of range, of corsor line, and last line of range.

: .-3 mark m | .+3 mark n | \
redraw! | echo line("'m") \
line(".") line("'n") "\n" \
exe "normal! " . \
"/\\%>'m" . "\\%<'n" . \
"PATTERN" . "\<CR>"

An alternative follows now. Here the line numbers for limits are dynamically evaluated:

: exe "normal! " . \
"/\\%>" . (line(".") -3) . "l" . \
"\\%<" .  (line(".") +3) . "l" . \
"PATTERN" . "\<CR>"

Here follows an alternative with line numbers for range limits stored in variables.

In three lines before the cursor line and in three lines (cursor line plus up to two lines after it) the matches will be highlighted.

And the cursor will be moved to the first match of PATTERN after cursor position.

By default highlighting does not skip the first character of first line of range but does it highlight if it is a match..

And by default the character after the corsor position is the first possible match (not where the cursor is).

: let a = line(".") -3 | \
let b = line(".") +3 | \
exe "normal! " . \
"/\\%>" . a . "l" . \
"\\%<" . b . "l" . \
"PATTERN" . "\<CR>"

This version of code makes it possible to use variables as search range limits. Thus it overrides the result of the mentioned link:

My faulty issue there, that vim does now allow to use variables as search range limits is now obsolete.

Regards

0

No, this syntax

/\%>.-1l\%<.+1lPATTERN

is not supported. The workaround is to calculate the expected line numbers directly and place those into the pattern:

let above='\%>' .. (line('.')-1)
let below='\%<' .. (line('.')+1)

exe '/' .. above .. below .. 'PATTERN'

Note even the /\%>.l is quite a recent addition, so does not work in older vims. It exists since patch 8.2.3110, so it's only there for 1.5 years (oh wow time passes, I actually thought it was even more recent :) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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