On 5/26/07, Iain Murray <[EMAIL PROTECTED]> wrote:
Hi,

Searching for "hello world" in vim doesn't match "hello\nworld".
This is annoying when editing documents. The solution is to
search for "hello\_sworld". But it is frustrating to type \_s
instead of a space in every search.

I have been getting around this by calling my own search function
(appended at the very end of this email). This issue has also
been discussed before:
    http://bugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=256743
and the solution then was to use a vim-script function too.

The problem with vim-script hacks is that they do not provide
incremental searching. I often find myself doing an incsearch,
having it fail and then having to try again with my own search
function. It might be possible to re-implement incsearch in
vim-script, but why reinvent the wheel? I think it would be
better if I could hook into vim's existing interactive search
mechanisms. For reference, emacs has an option to change the
meaning of ' ' in its incremental regex search (obtained with
M-C-s), the option is set in .emacs with
    (setq search-whitespace-regexp "[ \t\r\n]+")

I intend to write a patch for vim, if only for my own use. It
seems that I could do one of a few things:
    - add an option to allow search strings to be filtered
      through an arbitrary user function before being used
    - add a string option that would replace ' ' in searches.
    - add a Boolean option which offers one specific line-break
      ignoring behaviour.
    - extend the behaviour of the magic option (how?)
Which (if any) of these is likely to be adopted?

Perhaps these lines in doc/todo.txt are relevant? I don't really
understand them:
    From xvim: Allow a newline in search patterns (also for :s,
    can delete newline).  Add BOW, EOW, NEWL, NLORANY, NLBUTANY,
    magic 'n' and 'r', etc. [not in xvim:] Add option to switch
    on matches crossing ONE line boundary.
Can anyone tell me what xvim is, what the acronyms stand for and
what the reference to magic options means?

Thanks for your time,
Iain.


" This is the search function I have been using:
function! MySearch()
    let l:str = input('/')
    let l:str = substitute(l:str," ",'\\_s\\+',"g")
    let @/ = l:str
    call histadd('/',l:str)
    call search(l:str)
endfunction


I believe you can achieve what you want using 'cmap <expr>' cleverly.
Does this following do what you want ? You toggle special behaviour
of space in searching with F2, and you have incsearch, too.
Special-space is off in the following example, you can make it on
by inclugint this line: call ToggleSpecialSpaceSearch()

" to set ;special space in search' on by default, do this:
" call ToggleSpecialSpaceSearch()
nnoremap <silent><f2> :call ToggleSpecialSpaceSearch()<cr>
cnoremap <expr><f2> (ToggleSpecialSpaceSearch())
func! ToggleSpecialSpaceSearch()
   if exists('g:my_search') && g:my_search
       cunmap <space>
       let g:my_search=0
       echo 'space in search normal'
   else
       cnoremap <expr><space>
(getcmdtype()=='/'\|\|getcmdtype()=='?'?'\_s\+':' ')
       let g:my_search=1
       echo 'space in search special'
   endif
   return ''
endfun


Yakov

Reply via email to