Vim: how to make a macro/command that will wait for a key and use it?

command, macros, vim

Solution

You can use `getchar()`, for example:

nnoremap <F2> :call Fun()<CR>
function! Fun()
    let c = nr2char(getchar())
    if c=='q' || c=='k'
        exec 'normal fxs'.c.'j'
    endif
endfunction

Problem

For example, I want to temporarily map to fxsj. That is, when I press q, VIM will perform fxsqj. When I press k, VIM will perform fxskj. And so on.

Original source