How can I perform a search-and-replace and leave the cursor at the end of the replacement? I want to replace $...$ with \(...\) as I type in insert mode.
Notice that I can't use an abbreviation, since $ is replaced with two different strings.
The solution I've cobbled together so far is equivalent to:
function Foo()
let pat = '\$\(.\{-}\)\$'
if line =~ pat
execute 's/' . pat . '/\\(\1\\)/'
call cursor('.', matchend(getline('.'), '\\)'))
endif
endfunction
inoremap $ $<C-O>:call Foo()<CR>
Is there a better way to do this? The only alternative I found is to add the length of '\\)' to the column number from searchpos, and that isn't any nicer.