Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - How to insert a backslash at the start of a line in a new file using autocmd and a template file

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.

like image 861
Steve Biggs Avatar asked Nov 02 '15 18:11

Steve Biggs


1 Answers

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
like image 86
Matthew Strawbridge Avatar answered Oct 13 '22 00:10

Matthew Strawbridge