Created
July 28, 2023 13:11
-
-
Save StringManolo/ad8d2da9c68a51aa0ab53eff9e31a840 to your computer and use it in GitHub Desktop.
vimscript function to add custom emmet html snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Paste this code to the end of your .vimrc | |
" To expand my template write html:sm<tab> | |
" You can edit the html or create your own templates | |
" Add custom snippets: | |
function! AddHtmlSnippet(snippet) | |
let emmet_file = expand('~/.vim/plugged/emmet-vim/autoload/emmet.vim') | |
let emmet_content = readfile(emmet_file) | |
let start_pattern = "\\\\\\s*'html:5': \"<!DOCTYPE html>\\\\n\"" | |
let end_pattern = '\.\("<\/html>"\),' | |
let start_line = -1 | |
let end_line = -1 | |
for i in range(len(emmet_content)) | |
if emmet_content[i] =~ start_pattern | |
let start_line = i | |
endif | |
if start_line != -1 && emmet_content[i] =~ end_pattern | |
let end_line = i | |
break | |
endif | |
endfor | |
if start_line != -1 && end_line != -1 | |
let line_number = end_line + 1 | |
call insert(emmet_content, a:snippet, line_number) | |
call writefile(emmet_content, emmet_file) | |
endif | |
endfunction | |
" Add html:sm (sm == StringManolo), thats the template i usually use | |
let snippet_html_sm = "\\ 'html:sm': \"<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n<head prefix=\\\"og:http://ogp.me/ns#\\\">\\n\\t<meta charset=\\\"utf-8\\\">\\n\\t<link rel=\\\"icon\\\" href=\\\"data:;base64,iVBORw0KGgo=\\\">\\n\\t<title>Index.html</title>\\n\\t<meta property=\\\"og:type\\\" content=\\\"website\\\">\\n\\t<link rel=\\\"stylesheet\\\" href=\\\"./styles.css\\\">\\n\\t<meta name=\\\"theme-color\\\" content=\\\"#ffffff\\\">\\n</head>\\n\\n<body>\\n\\t|\\n\\n\\t<script src=\\\"./main.js\\\"></script>\\n</body>\\n</html>\"," | |
" Check if html:sm snippet already defined (avoid to append it again if it already exists) | |
if stridx(join(readfile(expand('~/.vim/plugged/emmet-vim/autoload/emmet.vim')), "\n"), 'html:sm') == -1 | |
call AddHtmlSnippet(snippet_html_sm) | |
endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment