Created
April 10, 2015 13:10
-
-
Save noahfrederick/5d5c20f938ff5df3c272 to your computer and use it in GitHub Desktop.
Vim NYC Meetup - 04/2015
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: