Vim Tips Wiki

Type and run templates instead of routine code

Redirected from VimTip710

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 710 Printable Monobook Previous Next

created May 4, 2004 · complexity basic · author akeru · version 6.0

There's a template script written in perl (see sources below).

Use this mapping in visual mode:

vnoremap <F6> :!perl E:\\Devtools\\vim\\vimfiles\\template\<CR>

The template markers are:

TS:<Single string template here>
<multistring template here>
TF: <template file name >

So now we are typing following templates and get them transformed.

Typing this:

TS:public final static int PROXY_TYPE_{$p2}={$p1};

Now select this template with values below, press magic button and 'ta-da', we have this code :

public final static int PROXY_TYPE_HTTP=1;
public final static int PROXY_TYPE_SOCKS=2;
public final static int PROXY_TYPE_SSH=3;
public final static int PROXY_TYPE_HTTPS=4;
public final static int PROXY_TYPE_NOPROXY=5;

Here's the same with multiline template, type following:

public {$p1} is{$p2}Established() \{
 return m{$p2};

and after transforming we'll got this :

protected boolean isDirectConnectionEstablished() {
  return m_DirectConnection;

protected boolean isSockConnectionEstablished() {
  return m_SockConnection;

protected boolean isHTTPConnectionEstablished() {
  return m_HTTPConnection;

I know it's stupid 'copy-paste' and I don't like it, but sometimes It's required and that simple script saving my time.

Text::Template you can download it from

use Text::Template;
$tb=0; $te=0;$template_str="";
while (<STDIN>){
 if (/^TF:(.*)$/){#file
 $template = Text::Template->new(SOURCE => $template_dir.$template_file)
 or die "Couldn't construct template: $Text::Template::ERROR";

 }elsif (/^TS:(.*)$/){#line
 $template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
 or die "Couldn't construct template: $Text::Template::ERROR";

 }elsif (/^TB:/){#line
 }elsif (/^TE:/){#line
 $template = Text::Template->new(TYPE=> STRING ,SOURCE => "$template_str\n")
 or die "Couldn't construct template: $Text::Template::ERROR";
 }elsif ($tb==1 && $te==0 ) {
 }else {

 if (defined $template) {
 @p=split /;/;
 #if ($p > 0) { print "$_\n";

 my %vars = (
 p1 => $p[0],
 p2 => $p[1],
 p3 => $p[2],
 p4 => $p[3],
 p5 => $p[4],
 p6 => $p[5],
 p7 => $p[6],
 p8 => $p[7],
 p9 => $p[8]
 my $result = $template->fill_in(HASH => \%vars);
 if (defined $result) { print $result }
 } #template_file defined
 }#else of TF
}#end while


I have something similar here

written in python, so it's inherently superior.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki