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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
</body>
</html>
Can I write a vim function do this is? Is there a better way?
-
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.
-
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.
Mark Biek : 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.innaM : I would have been nice if a specified the mode. You are absolutely correct.Mark Biek : This is definitely great for small stuff! -
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: aOr if you have this template already copied you can use
let @a = @*
To put this template to register 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>
Mark Biek : Can you think of a good cross-platform way to handle looking for the template files? I like this idea but I'd love to us it on Windows, Linux, & OSXMark 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.Luc Hermitte : That's were plugins come in... -
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.Mark Biek : I liked the sound of this plugin but I could never actually get it to work.Mark Biek : I'll check out snipMate, thanks! -
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-filehtml/whatever.template
(instead ofhtml.template
), and load it with:MuTemplate html/whatever
of withwhatever^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.
0 comments:
Post a Comment