Vim search template for text snippet captured in visual mode

I am trying to find the part of some string that twitches in visual mode.

What is the fastest way to do this in VIM? For exmaple

Hello, #{1} world. 

I press v to enter visual mode and select llo, #{1} wor in line 1. Then I pulled out the selected text by pressing y , and then, I try to search for the selected text by pressing / , This leads to the following questions:

A: Like past yanked text when I am in search mode?

Q: How to avoid the escape of characters for the search pattern?

+6
vim vi
source share
3 answers

I have exceeded the star command for visual mode (NB: this requires one file from lh-vim-lib). It answers your needs:

  • select in visual mode
  • press * / #
  • continue searching with n / n
+3
source share

BUT:

Ctrl-r 0 .

IN:

In addition to the trick, Ctrl-r 0 also has Ctrl-r = , which allows you to enter an expression that needs to be evaluated, and extends to the result.

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

Note that @reg means "the contents of the reg register", and register 0 means the last yank or delete. I think that avoids all characters that are special in vi regexps ... either way, you probably would rather do a match than type everything.

+12
source share

When you pull out any text (and do not specify a single register to embed), it goes into register 0. So, if you want to find this pulled text, press ESC to switch to normal mode, and then

 /CTRL-r0 

(i.e. press / , then CTRL + r , then 0 ) to display the contents of register 0 in the search pattern.

Some notes:

  • To search for other templates stored in other registers, you can enter :reg and look at the contents of the register before deciding which register content to use for your search.
  • To write to a register other than 0 (for example, 2), you can enter "2y ( : he v_y ).
  • To find the selected text directly, you can use the display here , which allows you to simply press X (upper case X ), and in visual mode, to search for that text.
  • For search in general, this vimcast gives you an introduction to a very powerful command line window with search history (I discovered it two weeks ago and absolutely love it!).
+6
source share

All Articles