1. JSON Handling in LunarVim
LunarVim streamlines JSON workflows with built-in tools, LSPs, and formatting. Here’s how to leverage it:
Key Features
- LSP Support: Auto-validation, syntax checks, and autocompletion via
json-lsp
. - Formatting: Built-in formatting with
null-ls
orjson-lsp
. - Keybindings:
gD
: Go to definition.K
: Hover documentation.=
(Normal/Visual mode): Format JSON (default keybinding).
JSON Formatting
Method 1: Built-in LSP Formatting
- Install
json-lsp
(if missing)::MasonInstall json-lsp
- Format the buffer:
- Entire file: Press
=
in Normal mode or run:lua vim.lsp.buf.format()
. - Selected lines: Highlight text in Visual mode and press
=
.
- Entire file: Press
Method 2: Prettier via null-ls
For advanced rules (e.g., enforcing quotes, trailing commas):
- Install Prettier globally:
npm install -g prettier
- Add to your
config.lua
:lvim.plugins = { { "jose-elias-alvarez/null-ls.nvim", config = function() require("null-ls").setup({ sources = { require("null-ls").builtins.formatting.prettier.with({ filetypes = { "json" }, }), }, }) end, }, }
- Format with
:lua vim.lsp.buf.format()
.
Method 3: Command-Line jq
Quickly prettify JSON using jq
:
- Format entire file:
:%!jq .
- Format selected lines:
- Highlight text in Visual mode.
- Run
:'<,'>!jq .
.
2. Folding JSON in Plain Vim (No Plugins)
Folding helps navigate deeply nested JSON structures. Use these methods in vanilla Vim:
Method 1: Indentation-Based Folding
Add to your .vimrc
:
autocmd FileType json setlocal foldmethod=indent
autocmd FileType json setlocal foldlevelstart=99
- Commands:
za
: Toggle fold under cursor.zR
/zM
: Open/Close all folds.zj
/zk
: Jump to next/previous fold.
Method 2: Syntax-Based Folding
autocmd FileType json setlocal foldmethod=syntax
- Relies on syntax rules (predefined in most JSON syntax files).
Method 3: Manual Folding
- Select text in Visual mode (
V
). - Create a fold with
zf
. - Delete folds with
zd
.
3. jq
-Like Filtering with jq-vim
Filter, transform, or calculate JSON data directly in Neovim using jq-vim
.
Installation (Lazy.nvim)
Add to your config.lua
:
lvim.plugins = {
{
"vito-c/jq.vim",
dependencies = { "jq" }, -- Install `jq` CLI first (e.g., `sudo apt install jq`)
ft = { "json" },
},
}
Usage
- Filter Selected JSON:
- Highlight text in Visual mode.
- Run
:'<,'>Jq .path.to.data
.
- Filter Entire File:
:%Jq ".data[] | select(.value > 10)"
- Output:
- Results appear in a split window.
Example Calculations
:%Jq "[.users[] | {name: .username, total: (.orders | length)}]"
4. CSV Handling Addendum
Manage CSV files with these plugins (install via Lazy.nvim):
Plugins
lvim.plugins = {
-- Pretty-print CSV as ASCII tables
{
"mechatroner/rainbow_csv",
ft = "csv",
},
-- CSV navigation and manipulation
{
"chrisbra/csv.vim",
ft = "csv",
},
}
Workflows
- Rainbow CSV:
:RainbowAlign
: Align columns.- Syntax highlighting by column.
- CSV.Vim:
:CSVSort 2
: Sort by column 2.:CSVSearch /pattern/
: Filter rows.
- Convert CSV to JSON:
:%!csvtojson | jq '.' # Requires `csvtojson` (npm install -g csvtojson)
5. Tree-sitter Explained
What Is Tree-sitter?
A parsing library that provides:
- Accurate syntax highlighting.
- Structural code navigation (e.g.,
]]
to jump between nodes). - Smart folding (
foldmethod=expr
).
Setup in LunarVim
LunarVim preinstalls nvim-treesitter
. Manage parsers with:
:TSInstall json # Install JSON parser
:TSPlayground # Visualize syntax tree
JSON-Specific Features
]k
/[k
: Jump to next/previous JSON key.:TSHighlightCapturesUnderCursor
: Debug syntax nodes.- Folding: Enable with:
vim.wo.foldmethod = "expr" vim.wo.foldexpr = "nvim_treesitter#foldexpr()"
Conclusion
- JSON: Format with
json-lsp
or Prettier, filter withjq-vim
, fold with Tree-sitter. - Folding: Use
indent
orsyntax
methods in vanilla Vim. - CSV: Leverage
rainbow_csv
andcsv.vim
for table workflows. - Tree-sitter: Enhances syntax analysis, navigation, and folding.
Configure with Lazy.nvim
for a seamless experience. 🌟