tags:

views:

44

answers:

1

When writing non-code with vim, I often have the need to search for multiple words that might be separated by a newline instead of a space.

For example, I may want to search for occurrences of "white house", but some occurrences may have a newline between "white" and "house".

I am aware that such multiline search is possible with "\_s" (e.g., "white\_shouse") but it is cumbersome. I would like to replace the search command such that spaces are treated as "\_s" without me having to type them out.

Is it possible to "remap" the / search command?

+1  A: 

You have at least two options:

  1. Define a mapping that will remap, for example, ,s to \_s: ,s is easier to type:
        cnoremap ,t \_s
    
  2. Define a custom search function that will replace all occurrences of \s with \_s and use it:
        function Search(prompt)
            let searchstring=substitute(input(a:prompt), '\\s', '\\_s', 'g')
            return a:prompt.searchstring."\n"
        endfunction
        nnoremap <expr> <special> / Search('/')
    
    or even
        function Search(prompt)
            let searchstring=substitute(input(a:prompt), ' ', '\\_s', 'g')
            return a:prompt.searchstring."\n"
        endfunction
        nnoremap <expr> <special> / Search('/')
    
    (this function replaces spaces with \_s)
ZyX