Vim - search for a template in the current line ONLY

I am wondering if there is a way to find the template, but limit it to the current line. Basically, the equivalent of /PATTERN , but limited to the current line, not the entire document.

I tried :s/PATTERN , but this removes the pattern from the line and places my cursor at the beginning of the line, which is not what I need at all. I was hoping you could search without replacement ...

I hope to use this as a macro to place my cursor at the beginning of this template, as it would when you do /PATTERN across the entire file, so anything that is macro-friendly is even better.

Any vim users who may have an idea?

EDIT: 0/PATTERN in the macro will work for my current need, but I hope there is a more specific way to limit the search.

ANSWER: There are several publishing methods here, but the one I like best right now uses Shift+V to visually display the current line, and then /\%V to search only in the visual selection. Then Shift+V will turn off the visual mode again.

+6
source share
4 answers

My macro knowledge is limited, but interactively you can select the current line with Shift + V and then do /\%Vsearch (see http://vimdoc.sourceforge.net/htmldoc/pattern.html#/ \% V).

+7
source
 /\%9lsearch 

Where \%9 means line number 9.

Entering the line number is still a little lame. You can ctrl + r = followed by vim and enter to evaluate the vim expression and insert its output. line('.') will return the cursor line.

In one complete step

 /\%<cr>=line('.')<cr>lsearch 

For more help see:

 :h /\%l :h i_CTRL-R 
+7
source

try to find the first character of the pattern by typing

 f <letter> 

This is not exactly what you need, but it can help solve the problem.

+4
source

You can search without replacement with

 :s/PATTERN//gc 

Then press n to skip the replacement. If the template is not found, you will not even be asked.

You can also simply select the current line or range of lines.

0
source

Source: https://habr.com/ru/post/945095/


All Articles