How to Reindent JSON File With a Shortcut in Sublime 4

There are many packages to reindent JSON files in Sublime 4. In this post I'll show how to install Pretty JSON of them and add key binding in order to use shortcut for formatting JSON files.

Step 1: Install Pretty JSON

To install Pretty JSON on Sublime Text 4 follow next instructions:

  • Open Package Control by CTRL + Shift + P
    • Or Preferences/Package Control
  • Search for package: Pretty JSON
  • Press on the package

If it's installed it showed be visible in:

  • Preferences
  • Package settings

Step 2: Add key binding/shortcut for JSON reindent

Next we are going to add shortcut to reindent JSON files by next combination: CTRL + Shift + J

To do so:

  • Preferences
  • Key Bindings
  • In the user key bindings (right window ) type the next:
{ "keys": [ "ctrl+alt+j" ], "command": "pretty_json" }

Or edit directly file:

~/.config/sublime-text-3/Packages/User/Default (Linux).sublime-keymap

or the version you have.

Now you can format your valid JSON files by pressing CTRL + Shift + J

Step 3: Customize JSON reindent in Sublime

Pretty JSON package allows you to change the way formatting is working. It can be done by:

  • Preferences
  • Package settings
  • Pretty JSON
  • To change any of the default settings - just copy and paste it in the right pane and change it.

For example you can use "indent": 2 by adding it in the right pane so it will become:

{
    "indent": 1
}

You can find default option below:

{
    "use_entire_file_if_no_selection": true,
    "indent": 4,
    "sort_keys": false,
    "ensure_ascii": true,
    "line_separator": ",",
    "value_separator": ": ",
    "keep_arrays_single_line": false,
    "max_arrays_line_length": 120,
    "pretty_on_save": true,
    "validate_on_save": true,
    "brace_newline": true,
    "bracket_newline": true,
    // Default: False
    // Valid Options: False, start, minimal
    "reindent_block": "minimal",
    // Name or Path to jq binary
    // Example: /usr/bin/local/jq
    "jq_binary": "jq",
    "jq_errors": false,
    "as_json":[
        "Packages/JSON/JSON.sublime-syntax",
        "Packages/PackageDev/Package/Sublime Text Commands/Sublime Text Commands.sublime-syntax",
        "Packages/PackageDev/Package/Sublime Text Settings/Sublime Text Settings.sublime-syntax",
        "Packages/PackageDev/Package/Sublime Text Menu/Sublime Text Menu.sublime-syntax"
 ]
}