souparno majumder
souparno majumder

Reputation: 2052

How do I search within a selected block of code with vim

say, I want to search for a particular text withing a function block. The present way, that i am implementing is selecting the block of code from within the function brackets with vi{ and then copying it and pasting it to a new file. After that I am searching for the text within the new file with /<search-text>

I want to know, if there is a short cut to this?

Upvotes: 11

Views: 7889

Answers (3)

AMcCracken
AMcCracken

Reputation: 153

I think this might be what you are looking for:

Limiting search scope for code in Vim

Using /\%Vsearch pattern should get you what you want after you have selected the block of code you wish to search in. You enter visual mode by hitting v and moving the cursor around to highlight the block you are searching in.

Upvotes: 10

Luc Hermitte
Luc Hermitte

Reputation: 32946

The almost exact same question has been asked last week on vi.SE.

While \%V can restrict the search to the current visually selected text (which is the precise answer to your question, but not to your indirectly expressed need), selecting the current function is much more tricky than a simple vi{. A perfect and simple way to select the current function requires scripting. That's where my answer on vi.SE kicks in.

Upvotes: 2

romainl
romainl

Reputation: 196566

vi{
:'<,'>g/foo/#

The '<,'> range is inserted automatically.

See :help range and :help :g.

Upvotes: 17

Related Questions