Ⅰ. 插件描述
Format js in vim via beautifyjs and spidermonkeyⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'zeusthetruegod/vim-format-js'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'zeusthetruegod/vim-format-js'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'zeusthetruegod/vim-format-js'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/zeusthetruegod/vim-format-js
Ⅳ. 文档说明
# Beautify your javascripts on the fly!
It is well known that a manual javascript formatting is very boring. Someone wrote a beautiful tool - js-beautify, but it never was properly integrated with vim. This one is the one I use for last 2 years.
How this works
- Press a <leader> ff( ,ff if your leader key is a comma) to format your javascript file when in a normal mode.
- Press a Ctrl-f inside an insert mode to format your javascript while you
are editing it. The cursor position is kept so you can continue typing the
code
- Press an 'U' to cancel this formatting if something goes wrong and you are
not happy with formatting
What do I need to have this working?
- Nodejs should be installed.
- Install this plugin any way you like (Pathogen / Vundle / Janus)
Why is this a great plugin?
- Marks and your current cursor position are kept during formatting. Here is how
I achieve this: during the js-beautify preprocessing number of non blank
characters is not changed, thus I associate a mark with the number of non blank
character before it and later put the cursor back.
- No need for extra configuration! expandtab and softtabstop already provides
me an info how you would like to format your javascript. TODO: You can always add an extra option s:formatjs_keep_blank_lines - Works without saving a file. Works in the indent mode.
TODO:
- Borrow css-beautify and html-beautify
Allow new lines inside () and [] for string parameters, because currently
[ 'a', 'b', 'c', 'd' ]
is always converted to the ['a','b','c','d'] . Sometimes it is important to
have word wraps here