Question: I followed this guide to automatically insert different header templates into new files of different types based on the file extension:

<http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/>

It works great! I have a custom header for python source files that gets inserted automatically when I open a new .py file.

I want to do a similar thing so that a basic LaTeX template is inserted when I open a new .tex file...

Except I can't get it to work...

My ~/.vimrc says this:

```
autocmd bufnewfile *.tex so /home/steve/Work/tex_template.txt

```

and my tex\_template.txt says this:

```
:insert
\documentclass[a4paper,12pt]{article}
.

```

but when I open a new file like this:

```
vim test.tex

```

(where test.tex does not exist already)

I get this:

```
"test.tex" [New File]
Error detected while processing /home/steve/Work/tex_template.txt:
line    2:
E492: Not an editor command: :insertdocumentclass[a4paper,12pt]{article}
Press ENTER or type command to continue

```

The problem appears to be with the backslash at the start of the line because if I delete the backslash from tex\_template.txt the the new file opens up with documentclass[a4paper,12pt]{article} in it. Except I need the backslash because otherwise it's not a tex command sequence.

Answer: 
If you look at `:help :insert` it says this:

> 
> Watch out for lines starting with a backslash, see
>  line-continuation.
> 
> 
> 

Following the link to `line-continuation` explains that the `\` is a continuation character which can be overridden by passing the `C` flag to `cpoptions`.

It should work if you change your template as follows:

```
:set cpo+=C
:insert
\documentclass[a4paper,12pt]{article}
.
:set cpo-=C

```
You might want to consider using a snippets engine like [vim-snipmate](https://github.com/garbas/vim-snipmate) or (my favorite) [ultisnips](https://github.com/sirver/ultisnips). With those you can insert snippets of text *everywhere*, not just at the beginning of a file.

As a bonus, these snippets can e.g. substitute variables and even run commands. The following is my snippet (for ultisnips) set to produce the header for a TeX file;

```
snippet hdr "File header for LaTeX" b
% file: `!v expand('%:t')`
% vim:fileencoding=utf-8:ft=tex
%
% Copyright © `!v strftime("%Y")` ${1:R.F. Smith} ${2:<my@email>}. All rights reserved.
% Created: `!p snip.rv = fcdate(path)`
% Last modified: `!v strftime("%F %T %z")`

$0
endsnippet

```

This will automatically fill in the file name and the time when the file was last modified. It fills in my name and e-mail with default values but gives me the opportunity to override them. The `fcdate` function is a piece of Python code that I wrote to retrieve the birthtime of a file.

I have the `hdr` snippet defined for several different filetypes, and a general one that is used for all other files. If I type `hdr``tab` at the beginning of a line, the appropriate snippet is expanded.