Skip to content

Instantly share code, notes, and snippets.

@qstrahl
Created August 27, 2015 16:25
Show Gist options
  • Save qstrahl/53c5d6c450b9d92198ac to your computer and use it in GitHub Desktop.
Save qstrahl/53c5d6c450b9d92198ac to your computer and use it in GitHub Desktop.
Make gq format your javascript with js-beautify
"" This file goes in after/indent/javascript.vim
function! MyJavascriptFormatter (lnum, count)
if mode() == 'i'
return 1
elseif synIDattr(synIDtrans(synID(line('.'), col('.'), 0)), 'name') == 'Comment'
return 1
endif
let l:options = []
let l:options += [ &expandtab ? '-s ' . &shiftwidth : '-t' ]
let l:options += [ '-w ' . &textwidth ]
let l:options += [ '-q' ]
let l:options += [ '-f -' ]
silent exe a:lnum . ',+' . (a:count - 1) . '!js-beautify ' . join(l:options, ' ')
undojoin | silent exe "'[,']retab!"
endfunction
if executable('js-beautify')
setlocal formatexpr=MyJavascriptFormatter(v:lnum,v:count)
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment