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 or json-lsp.
  • Keybindings:
    • gD: Go to definition.
    • K: Hover documentation.
    • = (Normal/Visual mode): Format JSON (default keybinding).

JSON Formatting

Method 1: Built-in LSP Formatting

  1. Install json-lsp (if missing):
    :MasonInstall json-lsp
  2. 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 =.

Method 2: Prettier via null-ls

For advanced rules (e.g., enforcing quotes, trailing commas):

  1. Install Prettier globally:
    npm install -g prettier
  2. 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,
      },
    }
  3. Format with :lua vim.lsp.buf.format().

Method 3: Command-Line jq

Quickly prettify JSON using jq:

  • Format entire file:
    :%!jq .
  • Format selected lines:
    1. Highlight text in Visual mode.
    2. 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

  1. Select text in Visual mode (V).
  2. Create a fold with zf.
  3. 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

  1. Filter Selected JSON:
    • Highlight text in Visual mode.
    • Run :'<,'>Jq .path.to.data.
  2. Filter Entire File:
    :%Jq ".data[] | select(.value > 10)"
  3. 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

  1. Rainbow CSV:
    • :RainbowAlign: Align columns.
    • Syntax highlighting by column.
  2. CSV.Vim:
    • :CSVSort 2: Sort by column 2.
    • :CSVSearch /pattern/: Filter rows.
  3. 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 with jq-vim, fold with Tree-sitter.
  • Folding: Use indent or syntax methods in vanilla Vim.
  • CSV: Leverage rainbow_csv and csv.vim for table workflows.
  • Tree-sitter: Enhances syntax analysis, navigation, and folding.

Configure with Lazy.nvim for a seamless experience. 🌟