Reputation: 3032
I want to execute the command "yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>"
by key sequence.
So I make a mapping
nnoremap <F7> yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>
This mapping copy the word under cursor, then the string :s/\<">/"/g" (where " are substituted by the copied word) appears in the command line and the cursor in the command line is at the end of replacement statement.
I also want to save cursor position before this command and restore after.
function! SafeCommand(cmd)
let line = line('.')
let col = col('.')
// execute cmd here
call cursor( line, col )
endfunction
How to do that?
Upvotes: 1
Views: 226
Reputation: 172520
Normally, you'd just put the entire (complex) command in a function, and invoke that function from the :nnoremap
. But that doesn't work for incomplete commands, like the template :substitute
that your mapping represents. For that, you need to include the save / restore parts into the command-line (though that's ugly):
:fun! Save()
let s:line = line('.')
let s:col = col('.')
:endfun
:fun! Restore()
call cursor( s:line, s:col )
:endfun
:nnoremap <F7> yiw:call Save()<Bar>s/\<<C-r>"\>/<C-r>"/g<Bar>call Restore()<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
Upvotes: 1