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?
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.
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.
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 .
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>
vi{
:'<,'>g/foo/#
The '<,'>
range is inserted automatically.
See :help range
and :help :g
.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With