Writing a vim function to insert a block of static text

FunctionVim

Function Problem Overview


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?

Function Solutions


Solution 1 - Function

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>

Solution 2 - Function

Late to the party, just for future reference, but another way of doing it is to create a command, e.g.

:command Inshtml :normal i your text here^V<ESC>

The you can call it as

:Inshtml

Explanation: the command runs in command mode, and you switch to normal mode with :normal, then to insert mode with 'i', what follows the 'i' is your text and you finish with escape, which is entered as character by entering ^V

It is also possible to add arguments, e.g.

:command -nargs=1 Inshtml :normal i You entered <args>^V<ESC>

where <args> (entered literally as is) will be replaced with the actual arguments, and you call it with

:Inshtml blah

Solution 3 - Function

i have created a new file generate_html.vim in ~/.vim/plugins/ with the following code

"
" <This plugin generates html tags to new html files>
"

autocmd	BufNewFile	*.html	call	Generate_html()

function! Generate_html()
	call append(0, "<!DOCTYPE HTML>")
	call append(1, "<html><head>")
	call append(2, "	<title></title>")
	call append(3, '	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />')
	call append(4, '	<style type="text/css">')
	call append(5, '	</style>')
	call append(6, '</head>')
	call append(7, '<body>')
	call append(8, '</body>')
	call append(9, '</html>')
endfunction

this way, everytime I open a new .html file in vim, it prints that text to the new file

Solution 4 - Function

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.

Solution 5 - Function

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.

Solution 6 - Function

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.

Solution 7 - Function

You can use Python (or any other program) if like me you haven't quite grasped vimScript

This guy talks about the vim expression register. Essentially you put something like this in your .vimrc file

:imap <C-j> <C-r>=system('/home/BennyHill/htmlScript.py')<CR>

So that every time in insert mode you press Ctrlj it calls the script htmlScript.py which has something like this in it (NOTE I haven't actually tested this)

#!/usr/bin/env python
import sys

snippet="""<!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>"""

sys.stdout.write(snippet)

Then just remember to make the file executable (chmod 0755 /home/BennyHill/htmlScript.py). It might be overkill, but I am far more comfortable with Python than I am with vim's syntax.

Solution 8 - Function

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.

Solution 9 - Function

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.

Solution 10 - Function

Put your text in a file (e.g.,html). Then put the following in your .vimrc.
command Inshtml r html

I use Inshtml instead of inshtml because vim doesn't support user defined command starting with small letter.

Then if can type Inshtml in command mode (i.e., :Inshtml).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark BiekView Question on Stackoverflow
Solution 1 - FunctionPaolo TedescoView Answer on Stackoverflow
Solution 2 - FunctionDan AndreattaView Answer on Stackoverflow
Solution 3 - FunctionuromayView Answer on Stackoverflow
Solution 4 - FunctionBrian AgnewView Answer on Stackoverflow
Solution 5 - Functionf3lixView Answer on Stackoverflow
Solution 6 - FunctioninnaMView Answer on Stackoverflow
Solution 7 - FunctionpukView Answer on Stackoverflow
Solution 8 - FunctionMykola GolubyevView Answer on Stackoverflow
Solution 9 - FunctionLuc HermitteView Answer on Stackoverflow
Solution 10 - FunctionalhelalView Answer on Stackoverflow