Vim - How to insert a backslash at the beginning 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 is automatically inserted when I open a new .py file.

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

Also, I can't get it to work ...

My ~ / .vimrc says the following:

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

and my tex_template.txt says the following:

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

but when I open a new file as follows:

 vim test.tex 

(where test.tex no longer exists)

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 is with the backslash at the beginning of the line, because if I remove the backslash from tex_template.txt, the new file is opened with documentclass [a4paper, 12pt] {article}. Also, I need a backslash, because otherwise it is not a tex command sequence.

+5
source share
2 answers

If you look :help :insert , it says the following:

Keep track of lines starting with a backslash; see linear continued.

Following the link to line-continuation , it is explained that the \ character is a continuation character that 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 
+4
source

You might want to use a fragment engine like vim-snipmate or (my favorite) ultisnips . With them, you can insert fragments of text everywhere, not just at the beginning of a file.

As a bonus, these fragments can, for example, replace variables and even run commands. Below is my snippet (for ultisnips) set to create a header for the TeX file;

 snippet hdr "File header for LaTeX" b % file: `!v expand('%:t')` % vim:fileencoding=utf-8:ft=tex % % Copyright Β© `!v strftime("%Y")` ${1:RF 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 the file was last modified. It fills my name and email with default values, but gives me the opportunity to override them. The fcdate function is a piece of Python code that I wrote to get the file’s birth time.

I have an hdr snippet defined for several different types of files, and a generic one that is used for all other files. If I type hdr tab at the beginning of the line, the corresponding fragment expands.

+1
source

Source: https://habr.com/ru/post/1235035/


All Articles