1

自动化创建 multimarkdown 注释的步骤。我希望 neovim 根据第一行的内容更改文件的文件类型。我所有的 multimarkdown 笔记都以titleEg开头

title: Euclidean Distance

理想情况下,我希望将其保留在我的 init.vim (.vimrc) 文件之外,但是当我将以下内容放入../ftplugin/txt.vim文件中时,neovim 不会在读取/打开时更新缓冲区。

" Change the file type to markdown
if getline(1) =~ '^title:'
   set ft=markdown
endif

如何让 neovim 检查文件的第一行并更改其类型或至少更改其语法。谢谢。

我了解运行时不会监视所有文件。自动检查文件类型并进行更改的唯一方法是通过 init.vim (.vimrc)使用autocmd和获取文件ftplugin/txt.vim

4

2 回答 2

2

根据:h new-filetypeB 部分,您可以执行以下操作:

augroup txt_to_markdown
    autocmd!
    autocmd BufRead * if &filetype == 'text && getline(1) =~ '^title:' | set filetype=markdown | endif
augroup END
于 2019-01-15T23:59:06.733 回答
2

这与@PeterRincker 的回答相同,但我认为您应该遵循:help new-filetype-scripts,因为描述(如果您的文件类型只能通过检查文件的内容来检测)与您的用例完美匹配。

这样,您将以下内容放入~/.vim/scripts.vim

if did_filetype()   " filetype already set..
    finish      " ..don't do these checks
endif
if getline(1) =~ '^title:'
    setfiletype markdown
endif
于 2019-01-16T07:53:53.413 回答