gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:02:49 25 4
gpt4 key购买 nike

我按照本指南根据文件扩展名自动将不同的标题模板插入到不同类型的新文件中:

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

它工作得很好!我有一个 python 源文件的自定义 header ,当我打开一个新的 .py 文件时,它会自动插入。

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

除了我无法让它工作......

我的 ~/.vimrc 是这样说的:

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

我的 tex_template.txt 是这样说的:
:insert
\documentclass[a4paper,12pt]{article}
.

但是当我打开一个这样的新文件时:
vim test.tex

(其中 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

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

最佳答案

如果你看 :help :insert它说:

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



点击链接至 line-continuation解释说 \是一个连续字符,可以通过传递 C 来覆盖它标志到 cpoptions .

如果您按如下方式更改模板,它应该可以工作:
:set cpo+=C
:insert
\documentclass[a4paper,12pt]{article}
.
:set cpo-=C

关于Vim - 如何使用 autocmd 和模板文件在新文件的行首插入反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33484348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com