views:

1450

answers:

6

I'd like to be able to do something like this in vim (you can assume v7+ if it helps).

Type in a command like this (or something close)

:inshtml

and have vim dump the following into the current file at the current cursor location

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <title></title>
    </head>
    <body>
    </body>
</html>

Can I write a vim function do this is? Is there a better way?

+2  A: 

Can you define an abbreviation. e.g.

:ab insh 'your html here'

as nothing in the above appears to be parameterised ?

In fact, looking at my VIM configs, I detect a new .html file being loaded thus:

autocmd BufNewFile *.html call GenerateHeader()

and define a function GenerateHeader() to insert my required template (I do the same for Java/Perl etc.).

It's worth getting the Vim Cookbook for this sort of stuff. It's a sound investment.

Brian Agnew
+3  A: 

This should be possible. I use auto-replacement. In my .vimrc I have this line:

iab _perls #!/usr/bin/perl<CR><BS><CR>use strict;<CR>use warnings;<CR>

And whenever I start a Perl script, I just type _perls and hit Enter.

innaM
Do you just type _perls in command-mode or is it :_perls ?
Mark Biek
OK, ignore that comment. I didn't understand that iab is the abbreviation command so you type _perls in insert mode.
Mark Biek
I would have been nice if a specified the mode. You are absolutely correct.
innaM
This is definitely great for small stuff!
Mark Biek
A: 

You can once copy this text to some ( for example 'a' ) register. And paste it every time you need unless you overwrite register 'a'.

To copy to register a in visual mode: "ay
To paste from register a in normal mode: "ap
To paste from register a in insert mode: a

Or if you have this template already copied you can use

let @a = @*

To put this template to register a.

Mykola Golubyev
+9  A: 

I do that by keeping under my vim folder a set of files which then I insert using the r command (which inserts the contents of a file, at the current location if no line number is passed) from some function:

function! Class()
    " ~/vim/cpp/new-class.txt is the path to the template file
    r~/vim/cpp/new-class.txt
endfunction

This is very practical - in my opinion - when you want to insert multi-line text. Then you can, for example, map a keyboard shortcut to call your function:

nmap ^N :call Class()<CR>
Paolo Tedesco
Mark Biek
OK, this is my favorite of all of them. iab is nice but it's hard to set up for complicated file templates. A cross-platform way to handle paths would still be nice though.
Mark Biek
That's were plugins come in...
Luc Hermitte
+3  A: 

snippetsEmu

I like to use the snippetsEmu vim plugin to insert code snippets like your.

The advantage of snippetsEmu is that you can specify place holders and jump directly to them in order to insert a value. In your case you could for example add a place holder between the title tags so you can easily add a title to the document when inserting this snippet.

snippetsEmu comes with various snippets (also for HTML) and new snippets can be esaily added.


EDIT

snipMate

Today I revisited my VIM confiugration + installed plugins and found the snipMate plugin, which is IMHO even better than snippetsEmu. snipMate updates just like TextMate all placeholders on the fly.

f3lix
I liked the sound of this plugin but I could never actually get it to work.
Mark Biek
I'll check out snipMate, thanks!
Mark Biek
A: 

There are many template expander plugins for vim.

NB: I'm maintaining the fork of muTemplate. Just dump your code into {rtp}/template/html.template or into $VIMTEMPLATES/html.template. And that's all. If you don't want the snippet to be implicitly loaded when opening a new HTML file, then name the template-file html/whatever.template (instead of html.template), and load it with :MuTemplate html/whatever of with whatever^r<tab> in INSERT mode (in an HTML buffer).

All the path issues, portability issues, etc are already taken care of. And unlike snippetEmu that supports (and somehow expects) several (hard to maintain, IMO) snippets in a same snippets definition file, Mu-template requires one template-file per snippet.

Luc Hermitte