Skip to content

Instantly share code, notes, and snippets.

@alvinfrancis
Last active December 19, 2015 17:28
Show Gist options
  • Save alvinfrancis/5991147 to your computer and use it in GitHub Desktop.
Save alvinfrancis/5991147 to your computer and use it in GitHub Desktop.
Simple VimL-Python script that uses sqlformat.org to format the selected string
" format SQL using sqlformat.org
function! SQLFormat() " {{{
'<,'>python << EOF
import vim
import urllib2, urllib, json
sql = ' '.join(vim.current.range)
params = {'sql': sql,
'n_indents': 4,
'keyword_case': 'upper',
'reindent': 1}
response = urllib2.urlopen('http://sqlformat.org/api/v1/format',
data=urllib.urlencode(params))
data = json.loads(response.read())
lines = str.splitlines(str(data['result']))
vim.current.range.append([line for line in lines if line != ''])
vim.command('\'<,\'>d')
EOF
endfunction " }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment