Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim search pattern for a piece of text line yanked in visual mode

Tags:

vim

vi

I am trying search a part of some line which is yanked under visual mode.

What's the quickest way to do it in VIM? For exmaple,

Hello, #{1} world.

I press v enter visual mode and select llo, #{1} wor at the line 1. Then I yanked the selected text by pressing y, and then, I am trying to search for the selected text by pressing /. That leads to the following questions:

A: How to past a yanked text when I am in search mode?

B: How to avoid the work of escaping characters for a search pattern?

like image 400
SunLiWei Avatar asked Jan 31 '11 06:01

SunLiWei


People also ask

How do I select a visual block 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 use visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.


3 Answers

A:

Ctrl-r 0.

B:

In addition to the Ctrl-r 0 trick, there's also Ctrl-r =, which lets you type in an expression to be evaluated and expands to the result.

/ (now the prompt looks like /)
Ctrl-R = (now the prompt looks like =)
escape(@0, '\^$*.~[') Enter (now the prompt looks like /llo, #{1} wor)
Enter

Note that @reg means "the contents of register reg", and register 0 is the the last yank or delete. I think that escapes all the characters which are special in vi regexps… in any case, you would probably prefer to make a mapping than to type that all in.

like image 134
ephemient Avatar answered Nov 15 '22 10:11

ephemient


When you yank some text (and specify no register to yank it into), it goes to register 0. So, if you want to search for that yanked text, press ESC to get into normal mode and then

/CTRL-r0

(i.e. press /, then CTRL+r, then 0) to pull the content of register 0 into the search pattern.

Some notes:

  • To search for other patterns stored in other registers, you could type :reg and watch the register contents before deciding which register content to use for your search.
  • To yank into a different register than 0 (e.g. 2), you could type "2y (:he v_y).
  • To search for the selected text directly, you could use the mapping described here which enables you to simply press X (uppercase character X) while in visual mode to search for that text.
  • For searching in general, this vimcast gives you an introduction to the very powerful command line window with the history of searches (discovered it two weeks ago and absolutely love it!).
like image 20
eckes Avatar answered Nov 15 '22 10:11

eckes


I've overriden the star command for the visual mode (NB: it requires one file from lh-vim-lib). It answers your need:

  1. select in visual mode
  2. press */#
  3. continue searching with n/N
like image 39
Luc Hermitte Avatar answered Nov 15 '22 08:11

Luc Hermitte