Writing a vim function to insert a block of static text

Solution 1:

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:

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