1

I'm trying to make some simple mappings that expand to more complex line mode commands, just to speed up some tasks. I would like to set where the cursor appears within the expanded command.

Take this as a trivial example:

cnoremap S!! system('')

Is there a way to define in that mapping, that the cursor should move to a position between the single quotes? (i.e. where _ is the cursor, make the command expand to system('_'))?

2

As this is a command mode mapping, you'll have to define the feature by yourself (IOW, I'm not aware of any snippet engines for command mode).

You should be able to do it by repeating <left> as many times as required.

cnoremap S!! system('')<left><left>

It should even be possible to automate it if you recognize a pattern and count automatically.

" untested code to give a rough idea of what could be done
function s:cmap_snippet(...) abort
    let keys = a:000[0]
    let snippet = join(a:000[1:], ' ')
    let p = match(snippet, '<++>')
    if p < 0
        let rhs = snippet
    else
        " not sure about the offsets
        let rhs = snippet[:p-1] . snippet[p+4:]
        let rhs .= repeat('<left>', strchars(snippet[p+4:]))
    endif
    exe 'cnoremap '.a:key.' '.rhs
endfunction

command! -nargs=+ CSnippetMap s:cmap_snippet(<f-args>) " or <q-args> or <args>, I never remember...

...

CSnippetMap S!! system('<++>')
1
  • Interesting idea, some bits I've never played with there. I'll have a look into it.
    – Chris
    Aug 16 '20 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.