Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Insert Open/Close Tag (With Current Word)
#!/usr/bin/env ruby
# This script will expand the current word into: <word></word>
# It will recognize HTML 4.0 tags that need no close tag.
# With no current word, it will insert: <p></p> and allows you
# to overwrite the tag name and add potential arguments.
# The result is inserted as a snippet, so it's
# possible to tab through the place holders.
# single tags
single_no_arg = /^(?:br|hr)$/i
single = /^(?:img|meta|link|input|base|area|col|frame|param)$/i
# we are not in HTML mode, so let’s scrap the above hardcoded tag lists
unless ENV.has_key? 'TM_HTML_EMPTY_TAGS' then
single_no_arg = /(?=not)possible/
single = /(?=not)possible/
# handle the case where caret is in the middle of a word, assume only the left part is the tag
tag, suffix =, ''
if index < tag.length && !ENV['TM_SELECTED_TEXT']
tag, suffix = tag[0...index], tag[index..-1]
xhtml = ENV['TM_XHTML'].to_s
print case tag
when single_no_arg then "<#{tag}#{xhtml}>"
when single then "<#{tag} $1#{xhtml}>"
when '' then "<${1:#{ENV['TM_DEFAULT_TAG'] || 'p'}}>$2</${1/\\s.*//}>"
else "<#{tag}>$1</#{tag.strip[/^\S+/]}>"
print suffix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment