Ⅰ. 插件描述
A better JSON for Vim: distinct highlighting of keywords vs values, JSON-specific (non-JS) warningsⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'sheerun/vim-json-the-thing-itself'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'sheerun/vim-json-the-thing-itself'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'sheerun/vim-json-the-thing-itself'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/sheerun/vim-json
Ⅳ. 文档说明
Better JSON for VIM
Distinct highlighting of keywords vs values, JSON-specific (non-JS) warnings. Customization of Jeroen Ruigrok van der Werven's vim-json highlighting script with Rogerz Zhang's indent script. Pathogen-friendly. Vundle-friendly too.
Specific customizations
- Added distinct highlighting for keywords vs values! (This is what made me start this plugin.)
- Added folding of
{...}and[...]blocks. To enable it:setlocal foldmethod=syntax(do it permanently on theftplugin/json.vimfile). JSON-specific warnings (red highlights):
- Warn about no-quotes in keywords and single-quotes in keywords and values.
- Warn about decimals smaller than 1 that don't start with a 0 (
.1gives a warning, it should be0.1). - Warn about comments
//and trailing semicolons;. - Warn about missing commas between elements of an object and elsewhere.
- Warn about trailing commas after the last element in arrays or objects.
- (All warnings can be turned off with a
let g:vim_json_warnings=0in yourvimrc.)
- Recognize
.jsonpfile type. In JSONP, the wrapping function call at the beginning and the closing semicolon are recognized.
Why use separate JSON highlighting instead of just Javascript?
Here's 2 compelling reasons:
- All JSON is Javascript but NOT all Javascript is JSON. So
{property:1}is invalid becausepropertydoes not have double quotes around it.{'property':1}is also invalid, because it's single quoted while the only thing that can placate the JSON specification is double quoting. JSON is even fussy enough that{"property":.1}is invalid too, because you should have of course written{"property":0.1}. Also, don't even think about having comments or semicolons, you guessed it: they're invalid. The point being that your syntax highlighter should warn you about these errors, in realtime, which is something that the Javascript highlighter doesn't (because in Javacript they're not errors!). - Distinct highlighting for keywords. JSON is an extremely lightweight data format but at its core lies an inescapable conceptual distinction: there are keywords and there are values. There's nothing much to the format besides that, so we might as well display keywords and values differently. This is something that gets lost with Javascript-inspired syntax highlighters, which see keywords as just another string since JSON requires them double quoted. So JSON files remain an impenetrable, indistinct wall of text.
Other recommended software
elzr's vim-json: Optionally adds JSON concealing as a feature
JSON Formatter: Chrome extension for printing JSON and JSONP nicely when you visit it 'directly' in a browser tab.