Thomson
Thomson

Reputation: 21694

Search in the current line in Vim?

I like the incremental search (/pattern) in vim, which matches the pattern when I am typing. But it is in file scope. Is it possible to limit it so search in the current line and wrap around when reaching the line end? This will help accelerate the navigation in one line, so it will be great if no extra (more) key strokes introduced.

Upvotes: 1

Views: 1168

Answers (4)

Arnie97
Arnie97

Reputation: 1069

Since Vim 8.2.3110 (July 2021), a new matching pattern was added for this:

\%.l    Matches at the cursor line.

So you can now use e.g. /keyword\%.l or /\%.lkeyword in latest Vim versions.

Upvotes: 0

romainl
romainl

Reputation: 196876

You can use fFtT,; to search single characters on the line:

f(
2T=

Or wWbBeE if you don't like thinking.

See :help navigation.

Upvotes: 3

Riff J
Riff J

Reputation: 51

From the pattern atoms in the vim documents, it looks vim doesn't support search in current line. But there are 2 other ways to do the similar things.

  • Select the current line first, then use \%V to search in seletion.
  • Use \%*l to search in specified line, like use /\%12laaa to search aaa in line 12.

There are also some other ways to search between marks, but it looks to be too complex to this scenario.

Upvotes: 4

FDinoff
FDinoff

Reputation: 31459

You could do something like this.

noremap <F6> /\%<C-R>=line('.')<CR>l

This uses the \%l atom to match the current line. For example \%15l matches line 15. This restricts the search to that line. Take a look :h \%l. To get the current line number we use the expression register and the line function.

Upvotes: 7

Related Questions