Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

search

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?

like image 294
isnvi23h4 Avatar asked Jun 02 '16 13:06

isnvi23h4


People also ask

How do I search for a selection in Vim?

When you press * ( Shift + 8 ) in Normal mode, Vim will search for the word (i.e. keyword ) under the cursor. Assuming the text you want to search is a single word, this is a great way to search.

How do I select a block of code in Vim?

Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block. Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement. Press d (delete) to cut, or y (yank) to copy.

How do I highlight and replace in Vim?

By pressing ctrl + r in visual mode, you will be prompted to enter text to replace with. Press enter and then confirm each change you agree with y or decline with n .

How do I change a block of text in Vim?

One may overwrite a visual-block of text with another visual-block of text by: Select the first block: ctrl-v move "ay. Select the second block: ctrl-v move c ctrl-o "aP <Esc>


2 Answers

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

The '<,'> range is inserted automatically.

See :help range and :help :g.

like image 94
romainl Avatar answered Nov 15 '22 10:11

romainl


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.

Hope this helps!

like image 36
AMcCracken Avatar answered Nov 15 '22 09:11

AMcCracken