That has a slight problem when your line does begin with "non word" chars (i.e: - _ ....)
I have a "smart home" command bound to the "Home" key. Repeated presses get you to the "smart" home and the "real" home.
Input: Selected Text or Line Output: Insert as Snippet
#!/usr/bin/ruby line = ENV['TM_CURRENT_LINE'] index = ENV['TM_LINE_INDEX'].to_i
# extract leading whitespace remain = line.sub(/(^\s*)/, '') whitespace = $1
if index == whitespace.size whitespace = '' else line = remain end
# Escape anything in the line that might be mistaken # as a snippet token. line = line.gsub(/([$\`])/){"\#$1"}
# place cursor print "#{whitespace}${1}#{line}"