├── pic.gif ├── plugin └── json_line_format.vim └── readme.md /pic.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/axiaoxin/vim-json-line-format/f4cf33c5a9f6b8361f6e55ca7cfd49cbe5da3620/pic.gif -------------------------------------------------------------------------------- /plugin/json_line_format.vim: -------------------------------------------------------------------------------- 1 | if !has("python") && !has("python3") 2 | echomsg "json-line-format need python support!" 3 | endif 4 | 5 | let pyEOF = "python << EOF" 6 | if !has("python") 7 | let pyEOF = "python3 << EOF" 8 | endif 9 | 10 | exec pyEOF 11 | 12 | import vim 13 | import json 14 | from collections import OrderedDict 15 | 16 | def json_line_format_print(): 17 | line_num = vim.current.window.cursor[0] 18 | try: 19 | formatted = json.dumps( 20 | json.loads(vim.current.line, object_pairs_hook=OrderedDict), 21 | ensure_ascii=False, indent=4) 22 | print(formatted) 23 | except Exception as e: 24 | print(e) 25 | 26 | def json_line_format_write(): 27 | line_num = vim.current.window.cursor[0] 28 | buff = vim.current.buffer 29 | try: 30 | formatted = json.dumps( 31 | json.loads(vim.current.line, object_pairs_hook=OrderedDict), 32 | ensure_ascii=False, indent=4) 33 | for i, line in enumerate(formatted.split('\n')): 34 | buff.append(line, line_num+i) 35 | vim.command('normal dd') 36 | except Exception as e: 37 | print(e) 38 | EOF 39 | 40 | if has("python") 41 | command JsonLineFormatWrite :python json_line_format_write() 42 | command JsonLineFormatPrint :python json_line_format_print() 43 | nnoremap wj :python json_line_format_write() 44 | nnoremap pj :python json_line_format_print() 45 | else 46 | command JsonLineFormatWrite :python3 json_line_format_write() 47 | command JsonLineFormatPrint :python3 json_line_format_print() 48 | nnoremap wj :python3 json_line_format_write() 49 | nnoremap pj :python3 json_line_format_print() 50 | endif 51 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Format json text line in VIM 2 | ================ 3 | 4 | _ ____ ___ _ _ _ ___ _ _ _____ _____ ___ ____ __ __ _ _____ 5 | | / ___| / _ \| \ | | | | |_ _| \ | | ____| | ___/ _ \| _ \| \/ | / \|_ _| 6 | _ | \___ \| | | | \| |___| | | || \| | _| ___| |_ | | | | |_) | |\/| | / _ \ | | 7 | | |_| |___) | |_| | |\ |___| |___ | || |\ | |__|___| _|| |_| | _ <| | | |/ ___ \| | 8 | \___/|____/ \___/|_| \_| |_____|___|_| \_|_____| |_| \___/|_| \_\_| |_/_/ \_\_| 9 | 10 | 11 | If you open a text by VIM which contents are json lines text, such as: 12 | 13 | {"question": "\u53d1\u5458\u5de5\u7684\u4e1c\u897f", "choices": ["\u6781\u6b63", "\u504f\u6b63", "\u4e2d\u6027", "\u504f\u8d1f", "\u6781\u8d1f", "\u975e\u8bc4\u4ef7"]} 14 | {"question": "\u6613\u8fc5\u8fd9\u6837\u6ca1\u6cd5\u6bd4", "choices": ["\u6781\u6b63", "\u504f\u6b63", "\u4e2d\u6027", "\u504f\u8d1f", "\u6781\u8d1f", "\u975e\u8bc4\u4ef7"]} 15 | {"question": "\u5f88\u591a\u4e1c\u897f\u5df2\u7ecf\u6bd4", "choices": ["\u6781\u6b63", "\u504f\u6b63", "\u4e2d\u6027", "\u504f\u8d1f", "\u6781\u8d1f", "\u975e\u8bc4\u4ef7"]} 16 | 17 | These json lines are inconvenient for human to read it, because of formating and encoding. Using this plugin, you can read these lines easily! 18 | 19 | ![](pic.gif) 20 | 21 | ### Install 22 | 23 | If you use Plug, add `Plug 'axiaoxin/vim-json-line-format'` in your `.vimrc`, then use `:PlugInstall` to install this plugin. 24 | 25 | Elseif you use Vundle, add `Plugin 'axiaoxin/vim-json-line-format'` in your `.vimrc`, then use `:PluginInstall` to install this plugin. 26 | 27 | Else copy `plugin/json_line_format.vim` to your `.vim/plugin` Manually. 28 | 29 | ### Usage 30 | 31 | Open a file in `Normal mode`, move your cursor on the json line, use `pj` to show formated json by print it, use `wj` could change the text to formatted json. 32 | 33 | Invalid json can not be formatted! 34 | 35 | Link: 36 | 37 | # Stargazers over time 38 | 39 | [![Stargazers over time](https://starchart.cc/axiaoxin/vim-json-line-format.svg)](https://starchart.cc/axiaoxin/vim-json-line-format) 40 | --------------------------------------------------------------------------------