0

I am using somebody else's vim script that has a neat plugin that allows me type in something like sub<tab> and it will generate the following for me:

sub ___ {
  ___
}

The blank lines act like fields. After the code is generated, the cursor is placed at the beginning of the first blank line and I can immediately type in the name of the subroutine. Then I hit the right arrow key and it takes me to the beginning of the next field and I start entering code for the subroutine. I call this a "mini template" for lack of a better term.

This is nice, but I would like the ability to generate different subroutine code for different kinds of subroutines. For example, sometimes I want a subroutine that starts like this:

# Example 1:
sub ___ {
  my ( $self, ___, ___ ) = validated_list(
      \@_,
      ___ => { isa => '___' },
      ___ => { isa => '___' },
  );
}

Other times I may want a subroutine that starts like this:

# Example 2:
sub ___ {
  my $self = shift;
  my ( ___, ___ ) = pos_validated_list(
      \@_,
      { isa => '___' },
      { isa => '___' },
  );
}

So I would like to have is something like abbreviations where I would type in sub1<TAB> and it would generate the first example and sub2<TAB> would generate the second example except it would provide these nice fields that I can fill out.

Are there any plugins that will help me easily generate these different mini templates?

5
  • If you post the example, that creates your existing template, it might be easier to adapt it to different types. Apr 21 '17 at 19:37
  • That part that goes in the .vimrc file looks like this: call SmartcomAdd( '^\s*sub', EOL, " ___ {\n___\n}", {'filetype':'perl'} )
    – StevieD
    Apr 21 '17 at 19:42
  • And where is SmartcomAdd defined and how is it defined? Apr 21 '17 at 19:43
  • I can add and modify templates but there is no way to generate different templates. I don't know how to modify the plugin to do that. It's a big hairy beast beyond my skills: github.com/thoughtstream/Damian-Conway-s-Vim-Setup/blob/master/… I figured something out there like this must exist. Like abbreviations on steroids.
    – StevieD
    Apr 21 '17 at 19:44
  • I'm thinking a poor man's way of doing this is I could do it using abbreviations. And instead blank lines I could put an asterisk. The abbreviation would move the cursor to the first insert point and I could type in the name of the subroutine. Then, using a hotkey, the cursor will jump to the next asterisk, delete it and then put me into insert mode so I could type in the next bit of code.
    – StevieD
    Apr 21 '17 at 19:55
1

The keyword you're looking for is snippet. There exist plenty snippet plugins. I'm maintaining mu-template (where snippets can use other snippets as functions call other functions), more popular plugins are snipmate & co.

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.