Giter VIP home page Giter VIP logo

vim-jsbeautify's Introduction

vim-jsbeautify - v1.1.3 - 2018-01-31

Build Status

Description

This extension allows you to use jsbeautifier inside vim to quickly format javascript, html and css files. Version 1.0 also supports the editorconfig file.

Any comments, corrections and suggestions are welcome.

Installation

Dependencies

Installing without plugin-manager

Download zip file or clone project. Then copy plugin folder from the plugin's directory to your ~/.vim folder.

unzip vim-jsbeautify-master.zip
cd vim-jsbeautify-master
cp -r plugin ~/.vim/

Then download the zip file for js-beautify and place its contents in ~/.vim/plugin/lib/, ie:

wget https://github.com/beautify-web/js-beautify/archive/master.zip && unzip master.zip && cp -rf js-beautify-master/ ~/.vim/plugin/lib/

Installing using pathogen

cd ~/.vim/bundle
git clone https://github.com/maksimr/vim-jsbeautify.git
cd vim-jsbeautify && git submodule update --init --recursive

Installing using vundle

  1. Add this line to your .vimrc:
Plugin 'maksimr/vim-jsbeautify'
  1. Call PluginInstall() inside vim:
:PluginInstall
  1. Update git submodules: cd ~/.vim/bundle/vim-jsbeautify && git submodule update --init --recursive

Settings

In version 1.0, all configuration is done through the .editorconfig file. This file can be located either in the user home directory ~/.editorconfig, or in inside the vim configuration directory ~/.vim/.editorconfig.

To assign a custom path for the .editorconfig file set the variable g:editorconfig_Beautifier.

Settings are taken from sections [**.js], [**.json], [**.jsx], [**.html], and [**.css]. Within these sections can use a special comment ; vim:, but this comment can be used only for the global settings.

You can also configure a variable g: config_Beautifier(g:config_Beautifier has type dict), but it is better to use the .editorconfig file.

Examples

A simple example of the .editorconfig file:

;.editorconfig

root = true

[**.js]
indent_style = space
indent_size = 4

[**.json]
indent_style = space
indent_size = 4

[**.jsx]
e4x = true
indent_style = space
indent_size = 4

[**.css]
indent_style = space
indent_size = 4

[**.html]
indent_style = space
indent_size = 4
max_char = 78
brace_style = expand

The .editorconfig file uses special comments (;vim:) and special properties for the jsbeautify plugin like path, bin

;.editorconfig

root = true

[**.js]
; path to optional external js beautifier, default is vim-jsbeautify/plugin/lib
path=~/.vim/bundle/js-beautify/js/lib/beautify.js
; Javascript interpreter to be invoked by default 'node'
bin=node
indent_style = space
indent_size = 4

[**.css]
path=~/.vim/bundle/js-beautify/js/lib/beautify-css.js
indent_style = space
indent_size = 4

[**.html]
; Using special comments
; And such comments or apply only in global configuration
; So it's best to avoid them
;vim:path=~/.vim/bundle/js-beautify/js/lib/beautify-html.js
;vim:max_char=78:brace_style=expand
indent_style = space
indent_size = 4

Usage

".vimrc
map <c-f> :call JsBeautify()<cr>
" or
autocmd FileType javascript noremap <buffer>  <c-f> :call JsBeautify()<cr>
" for json
autocmd FileType json noremap <buffer> <c-f> :call JsonBeautify()<cr>
" for jsx
autocmd FileType jsx noremap <buffer> <c-f> :call JsxBeautify()<cr>
" for html
autocmd FileType html noremap <buffer> <c-f> :call HtmlBeautify()<cr>
" for css or scss
autocmd FileType css noremap <buffer> <c-f> :call CSSBeautify()<cr>

JsBeautify function takes two parameters: the starting line number and the ending line number. By default these parameters are set to 0 and $.

If you want to beautify only selected lines you should use functions RangeJsBeautify, RangeJsonBeautify, RangeJsxBeautify, RangeHtmlBeautify, or RangeCSSBeautify.

Example of binding a function for js, html and css in visual mode on

".vimrc
autocmd FileType javascript vnoremap <buffer>  <c-f> :call RangeJsBeautify()<cr>
autocmd FileType json vnoremap <buffer> <c-f> :call RangeJsonBeautify()<cr>
autocmd FileType jsx vnoremap <buffer> <c-f> :call RangeJsxBeautify()<cr>
autocmd FileType html vnoremap <buffer> <c-f> :call RangeHtmlBeautify()<cr>
autocmd FileType css vnoremap <buffer> <c-f> :call RangeCSSBeautify()<cr>

Running tests

Tests are written in Urchin for vim files and built using Grunt test runer for javascript files. Note that all the commands bellow you should run from the plugin directory.

Run vim tests with urchin:

./urchin test/vim

(Or put it some other place in your PATH.)

How to run vim and javascript tests with grunt:

npm install -g grunt-cli
npm install

grunt test

Run only javascript tests:

grunt nodeunit

Thanks for assistance:

Versions

v1.1.3

  • Add support js-beautify v1.7.5

v1.1.2

  • Add support js-beautify v1.6.3

v1.1.1

  • Add support js-beautify v1.3.1

License

Licensed MIT Copyright (c) 2018 Maksim Ryzhikov;

vim-jsbeautify's People

Contributors

beforeload avatar christiangenco avatar edmistond avatar fvictorio avatar ibash avatar ifokeev avatar jetz avatar johvik avatar livelazily avatar lvarayut avatar maksimr avatar matthewbednarski avatar posva avatar sahands avatar sbdchd avatar shihshen avatar sullyj3 avatar tony avatar xasos avatar xuhdev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.