开发者

Overwriting search command in vim

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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜