[runtime] Better file type detection for YAML files

17 views
Skip to first unread message

Franklin, Jason

unread,
Mar 5, 2020, 3:50:31 PM3/5/20
to vim...@googlegroups.com
Greetings:

YAML files are only detected when they end in ".yml" or ".yaml".

The change below allows for YAML to be detected by content:

diff --git a/runtime/scripts.vim b/runtime/scripts.vim
index a69043101..71fc56a48 100644
--- a/runtime/scripts.vim
+++ b/runtime/scripts.vim
@@ -376,6 +376,10 @@ else
elseif s:line1 =~? '-\*-.*erlang.*-\*-'
set ft=erlang

+ " YAML
+ elseif s:line1 =~# '^\(---\|%YAML\)'
+ set ft=yaml
+
" CVS diff
else
let s:lnum = 1

If a file begins with "---" we can assume that this is a YAML document
boundary symbol. One may argue that this is not for sure, but it seems
unlikely to me that any other type of content would begin this way.

The other possibility is when some text begins with a YAML version
directive (e.g., "%YAML 1.2"). With this criteria we can be nearly
certain of the type.

This doesn't handle all cases. However, it will catch far more
instances where the content of a file is YAML, yet the name doesn't
match what is expected.

Thanks!

--
Jason Franklin

Bram Moolenaar

unread,
Mar 6, 2020, 4:31:53 PM3/6/20
to vim...@googlegroups.com, Franklin, Jason
Recognizing "%YAML" should be fine. However, it's not unusual for a
text file to start with something like:

-----------------------------
blah blah blah blah
-----------------------------

I rather not see that as a yaml file.


--
hundred-and-one symptoms of being an internet addict:
181. You still remember the "happy tune" your modem made
while dialing your ISP.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages