liaogang
liaogang

Reputation: 518

How to search two word in Vim not using "/"?

I want to custom a macro in vim include a function to search a group of words.
Is it possible?

i.e.

The input values can't be empty. (1)
    ^
The input value is five.(2)
    ^
The input values is unknown.(3)
    ^

The cursor is in (1). I want to search "input values".
Error search: "input value" at (2).
Expected : The cursor is pointer to "input values" at (3).

Upvotes: 0

Views: 90

Answers (2)

frank.lin
frank.lin

Reputation: 1694

nnoremap \g v2e:call VisualSearch()<CR>

function! VisualSearch() range
    let l:saved_reg = @"
    execute "normal! vgvy"
    let l:pattern = escape(@", '\\/.*$^~[]')
    let l:pattern = substitute(l:pattern, "\n$", "", "")
    execute "normal /" . l:pattern . "^M"
endfunction

v2ewill choose two words(only your cursor is on the begin of the first word ) in visual mode;

then :call VisualSearch()<CR> , call the function VisualSearch;

VisualSearch will search words you select in visual mode

it works for me.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172570

So, if I understand you right, you want something like the * / # commands, but not for the current word, but for more / different text?!

Several plugins extend the * / # commands to visual mode. With them, you can select the two words (input values) and then search for them via v2e*). One such plugin is my SearchHighlighting plugin, which also offers additional features. (The plugin page has links to alternative plugins.) The visualstar.vim plugin provides just that single feature.

Upvotes: 3

Related Questions