Vim - 如何使用autocmd和模板文件在新文件的行开头插入反斜杠

Ste*_*ggs 5 vim latex tex autocmd

我按照本指南自动将不同的头模板插入到基于文件扩展名的不同类型的新文件中:

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

它很棒!我有一个python源文件的自定义标头,当我打开一个新的.py文件时会自动插入.

我想做一个类似的事情,以便在我打开一个新的.tex文件时插入一个基本的LaTeX模板...

除了我不能让它工作......

我的〜/ .vimrc说:

autocmd bufnewfile *.tex so /home/steve/Work/tex_template.txt
Run Code Online (Sandbox Code Playgroud)

我的tex_template.txt说:

:insert
\documentclass[a4paper,12pt]{article}
.
Run Code Online (Sandbox Code Playgroud)

但是当我打开一个像这样的新文件时:

vim test.tex
Run Code Online (Sandbox Code Playgroud)

(其中test.tex尚不存在)

我明白了:

"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
Run Code Online (Sandbox Code Playgroud)

问题似乎是在行开头的反斜杠,因为如果我从tex_template.txt中删除反斜杠,则新文件打开,其中包含documentclass [a4paper,12pt] {article}.除非我需要反斜杠,否则它不是tex命令序列.

Mat*_*dge 4

如果你看一下:help :insert它是这样说的:

注意以反斜杠开头的行,请参阅行继续。

下面的链接解释line-continuation了 是\一个连续字符,可以通过将C标志传递给 来覆盖它cpoptions

如果您按如下方式更改模板,它应该可以工作:

:set cpo+=C
:insert
\documentclass[a4paper,12pt]{article}
.
:set cpo-=C
Run Code Online (Sandbox Code Playgroud)