0

Task:

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
104 
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.

Attempt:

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

Question:

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

3
  • 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, 2018 at 14:51
  • 1
    (Im ignoring the plugin solutions, of which there are many)
    – D. Ben Knoble
    Jul 28, 2018 at 14:52
  • What in particular do you find ugly about your solution?
    – Rich
    Jul 30, 2018 at 14:46

1 Answer 1

1

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

    end
  end
endsnippet

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 <++>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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