Skip to content

Instantly share code, notes, and snippets.

@noahfrederick
Created April 10, 2015 13:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahfrederick/5d5c20f938ff5df3c272 to your computer and use it in GitHub Desktop.
Save noahfrederick/5d5c20f938ff5df3c272 to your computer and use it in GitHub Desktop.
Vim NYC Meetup - 04/2015
" jsbeautify.vim - Wrapper for js-beautify command-line utility
"
" See js-beautify -h for available options.
if !executable('js-beautify')
finish
endif
function! s:JsBeautify() range abort
if &filetype ==# 'javascript' || &filetype ==# 'json'
let type = 'js'
else
let type = &filetype
endif
let cmd = [
\ '!js-beautify',
\ '--file -',
\ '--type',
\ type,
\ ]
if (type ==# 'js' || type ==# 'html') && &textwidth > 0
let cmd = add(cmd, '--wrap-line-length ' . &textwidth)
endif
if type ==# 'html'
let cmd = add(cmd, '--indent-inner-html')
endif
if &expandtab
let cmd = add(cmd, '--indent-size ' . &shiftwidth)
else
let cmd = add(cmd, '--indent-with-tabs')
endif
execute a:firstline . ',' . a:lastline . join(cmd)
endfunction
augroup jsbeautify
autocmd!
autocmd FileType css,html,javascript,json
\ command! -nargs=0 -range=% -buffer JsBeautify <line1>,<line2>call s:JsBeautify()
augroup END
" vim: fdm=marker:sw=2:sts=2:et
" marks.vim - Friendly :marks
function! s:Marks() abort
try
marks abcdefghijklmnopqrstuvwxyz.
catch /^Vim\%((\a\+)\)\=:E283/
echo 'No marks'
return
endtry
echo 'Jump to mark (<Space> cancels): '
let mark = nr2char(getchar())
" Dismiss "Press ENTER or type command to continue" prompt
redraw
if mark !=# ' '
execute 'normal! `' . mark
endif
endfunction
command! -nargs=0 -bar Marks call s:Marks()
" vim: fdm=marker:sw=2:sts=2:et
@Asheq
Copy link

Asheq commented Jul 27, 2017

I use the :JsBeautify command on a regular basis. The code here is simple and transparent. Thanks :)

A note to others who use editorconfig settings like I do:
If you would like to use editorconfig settings, you need to pass the --editorconfig flag to js-beautify, and you need to temporarily change Vim's current working directory to the buffer's directory so that js-beautify can properly look for .editorconfig files in parent directories. I altered Noah's JsBeautify function to look like this:

function! s:JsBeautify() range abort
    " Set Vim's current working directory to directory of current file so that --editorconfig flag works properly
    cd %:p:h

    " Run js-beautify
    if &filetype ==# 'javascript' || &filetype ==# 'json'
        let type = 'js'
    else
        let type = &filetype
    endif
    let cmd = [
                \ '!js-beautify',
                \ '--editorconfig',
                \ '--type',
                \ type,
                \ ]
    execute a:firstline . ',' . a:lastline . join(cmd)

    " Reset current working directory
    cd -
endfunction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment