繁体   English   中英

在Vim中,是否可以基于要打开的文件中的第一行设置变量?

[英]In Vim, is it possible to set a variable based on the first lines in the file that is being opened?

我希望vim从文件开头检测并存储带注释的行数(以#开头),然后使用具有存储值的变量以特定于文件的方式更改vim设置。

例:

如果我打开此文件:

# Comment here
# and here
file text starts here

我希望变量存储2 ,然后使用该值设置csv_headerline=一行开始突出显示(特别是在csv-vim软件包中使用csv_headerline= )。 注释行的数量将在文件之间变化。

我认为可能有一种方法可以使用autocmd并设置我的.vimrc以使vim在打开文件之前先浏览文件的前几行,但我不知道该怎么做。

以@TesselatingHeckler的评论为指导,我在.vimrc添加了以下内容:

" Detect comments and identify the header line in csv-files                                                                                                                                                         
autocmd Filetype csv /^[^#] " Place cursor on first non-comment line                                                                                                                                                 
autocmd Filetype csv let b:csv_headerline=line('.') " Set cursor line as csv_headerline                                                                                                                              
autocmd Filetype csv CSVInit " Update vim.csv plugin to be aware of the new headerline                                                                                                                               

(将这些命令直接传递到vim -c我不得不写line('.')+1 ,不知道为什么)。

到目前为止,它只适用于文件开头的注释行以及注释行和空行的混合使用(有限的测试)!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM