on key press, lets say <C-G> print a basic skeleton, e.g.:

 99 control '<++>' do
100   impact '<++>'
101   title '<++>'
102   desc '<++>'
103   describe <++>(<++>) do
105    end
106  end

There is a tone of info for how to do that on a file creation but I'm not interested in that.


What I've done:

  4 set nocompatible
  5 filetype plugin on
  6 syntax on
 10 inoremap <C-G> control '<++>' do<Esc>"_o<Space><Space>impact '<++>'<Esc>"_o<Space><Space>title<Space>'<++>'<Esc>"_o<Space><Space>desc<Space>'<++>'<Esc>"
    _o<Space><Space>describe<Space><++>(<++>)<Space>do<Esc>"_o<Esc>"_o <Space><Space>end<Esc>"_o end


It works but it's ugly. Is there a way to do it better?

  • 1
    Store the template somewhere and :read it in ; store the template as a variable and paste it with the = register ; use a macro ... – D. Ben Knoble Jul 28 '18 at 14:51
  • 1
    (Im ignoring the plugin solutions, of which there are many) – D. Ben Knoble Jul 28 '18 at 14:52
  • What in particular do you find ugly about your solution? – Rich Jul 30 '18 at 14:46

As this probably won't be the only part of code you'll repeat, and you'll probably want to replace the <++> with some other text, a snippet plugin will be most useful to you.

Plug in UltiSnips, open the file you want to add the skeleton to, and use :UltiSnipsEdit. This will open you a buffer where you can add all sorts of snippets.

snippet s "My skeleton"
 control '<++>' do
   impact '<++>'
   title '<++>'
   desc '<++>'
   describe <++>(<++>) do


Here, s is the activator for the following snippet, the string in quotes is a description (shown e.g. by YCM). The activator can be a word, if it will be easier to you.

Save that buffer from above and enter the activator string in your file. Hit enter and you'll get your snippet in your buffer.

You can read more here - :help UltiSnips-plaintext-snippets about how to setup more sophisticated snippets to fit all of your needs, including a visual placeholder e.g. to quickly switch between the <++>.

| improve this answer | |

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.