Quickly reshape JSON and escaped strings (certificates, keys, config values) right inside your buffer.
-
Install (lazy.nvim):
{ "yourname/nvim-mutate", config = true } -
Open a file, then run any of the commands below. With no selection they act on the whole buffer; select some lines first (visual mode) to act on just the selection.
:MutateJsonFormat " pretty-print the whole file :'<,'>MutateNewlinesExpand " expand the selected one-line cert
That's it.
| Command | What it does |
|---|---|
:MutateJsonFormat |
Pretty-print JSON with indentation. |
:MutateJsonMinify |
Collapse JSON onto a single line. |
:MutateNewlinesExpand |
Turn escaped \n (and \r\n) into real line breaks. |
:MutateNewlinesCollapse |
Turn real line breaks back into escaped \n. |
Every command works on the visual selection when you run it with a range
(:'<,'>Command), and on the entire buffer otherwise.
You have a JSON value like this and want to actually read it:
"cert": "-----BEGIN CERTIFICATE-----\nMIIB...\n-----END CERTIFICATE-----\n"Select that line and run :'<,'>MutateNewlinesExpand:
"cert": "-----BEGIN CERTIFICATE-----
MIIB...
-----END CERTIFICATE-----
"When you're done editing, select the lines and run
:'<,'>MutateNewlinesCollapse to squeeze it back onto one line.
Turn a compact blob into something readable with :MutateJsonFormat:
{"name":"demo","tags":["a","b"],"nested":{"on":true}}becomes
{
"name": "demo",
"tags": [
"a",
"b"
],
"nested": {
"on": true
}
}Run :MutateJsonMinify to go back to a single line — handy before pasting into
another config file.
Optional. The only setting is the indent width used by :MutateJsonFormat:
require("nvim-mutate").setup({
indent = 2, -- spaces per level (default)
})make test # run the test suite (fetches plenary.nvim into .tests/ if needed)