Tuesday, July 20, 2010

Re: Default search changed by search and replace

Am 19.07.2010 18:16, schrieb Mark Butler:
> The behavior of the 'n' command appears to have recently changed. It
> used to always repeat the last _search_. Now it appears to repeat the
> search implied by the last _search and replace_, when there has been
> an intervening search and replace since the last search.
>
> This is extremely annoying. It is common to search for lines where you
> need to do a search and replace and where what you are searching for
> and what you are replacing is different.
>
> I am not familiar with any vi derivative that has previously behaved
> this way. Is there an option to make vim behave the way it used to?
> Or would perhaps a patch to restore the old behavior be considered?
>
> I am using vim 7.0.109 on Centos 5.5, by the way.

I use a dummy command :InFunc to wrap the execution of a :substitute (or
similar command) in a function. How it works:
:h function-search-undo

Usage:
:[range]InFunc[!] {cmd}

A bang "!" ignores the range. Default range is the current line.

Examples:
" replace in visual selection:
:'<,'>InFunc s/mouse/deer/g

" the same (ignore '%'):
:%InFunc! '<,'>s/mouse/deer/g

:InFunc! g/func/#

Here is the command:

com! -bang -range -nargs=+ InFunc <line1>,<line2>call InFunc(<bang>0, <q-args>)

func! InFunc(bang, cmd) range
if a:bang
exec a:cmd
else
exec a:firstline.",".a:lastline. a:cmd
endif
endfunc


" Examples from the vimrc:
" (some commands get two mappings: one 'do it immediately'-mapping and
" one 'let me press <Enter> myself'-mapping)

cno <expr> <SID>% getcmdpos()==1 ? "%" : ""

" remove trailing ^M
no <script> <Leader>sm :<SID>%InFunc s/\r$//e
sunmap <Leader>sm
ounmap <Leader>sm
map <Leader>s<CR> <Leader>sm<CR>
sunmap <Leader>s<CR>
ounmap <Leader>s<CR>

" remove trailing spaces
no <script> <Leader>ss :<SID>%InFunc v/^-- /s/\s\+$//e
sunmap <Leader>ss
ounmap <Leader>ss
map <Leader>s<Space> <Leader>ss<CR>
sunmap <Leader>s<Space>
ounmap <Leader>s<Space>

" remove blank lines (leave one)
no <script> <Leader>sj :<SID>%InFunc g/^\s*\n\s*$/norm! dvip0D
sunmap <Leader>sj
ounmap <Leader>sj

--
Andy

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: