Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - Find pattern on currently line ONLY

Tags:

vim

macros

I'm wondering if there is a way to find a pattern, but restrict it to the current line. Basically, the equivalent of /PATTERN but restricted to the current line, rather than the entire document.

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

I'm hoping to use this for a macro in order to place my cursor at the start of that pattern, as would happen when you do /PATTERN on the entire file, so anything that is macro-friendly is even better.

Any vim users out there that might have an idea?

EDIT: 0/PATTERN in a macro would work for my current need, but I'm hoping there's a more specific way to restrict the search.

ANSWER: There's a few ways posted in here so far, but the one I like best right now is using Shift+V to select the current line visually, followed by /\%V to search only in the visual selection. Then Shift+V again will turn off the visual mode.

like image 353
Austin Avatar asked May 15 '13 18:05

Austin


4 Answers

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

like image 119
joon Avatar answered Oct 27 '22 01:10

joon


try to Find first character of the Pattern by typing

f <letter>

It's not exactly what you need but can help to solve the problem.

like image 22
KolobocK Avatar answered Oct 27 '22 00:10

KolobocK


/\%9lsearch

Where \%9 means line number 9.

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

In one complete step

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

For more help see:

:h /\%l
:h i_CTRL-R
like image 28
Peter Rincker Avatar answered Oct 26 '22 23:10

Peter Rincker


  1. Place the cursor on the line you want to search in
  2. Select it with shift+v
  3. Type / to begin searching
  4. Prefix your term with \%V, e.g. \%Vabc to search for abc in only the visually selected blocks (in our case the single line)

vim screencast

like image 41
alex Avatar answered Oct 27 '22 01:10

alex