Sublime Text - JSON formatter shortcut
Asked Answered
R

9

212

I'm using SublimeText.

How to reindent Json code with a shortcut?

I've already installed packageControl and it works.

I already tried JsonReindent package but it has not a shortcut by default and i don't know its command name.

I already have this command (from my previous sublimetext2 on another pc, where it works fine):

// JSON reindent
{ "keys": ["ctrl+shift+j", "ctrl+shift+j"], "command": "json_reindent" },

But here it doesn't work.

Rozamond answered 20/1, 2016 at 9:58 Comment(0)
S
12

If you look at the source, the name of the sublime_plugin.TextCommand subclass is SublimeJsonReindentCommand. Per the conventions for command names, the resulting command is named sublime_json_reindent. Simply change your key binding to:

{ "keys": ["ctrl+shift+j", "ctrl+shift+j"], "command": "sublime_json_reindent" },

and you should be all set.

Song answered 20/1, 2016 at 14:43 Comment(5)
The command does nothing. In the console nothing appears. If i put the command directly in the console: >>> sublime_json_reindent Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'sublime_json_reindent' is not definedRozamond
@Rozamond in the console, run view.run_command("sublime_json_reindent") and let me know what happens. Make sure you have a JSON file open and some text selected.Song
@Rozamond are you sure you have JSON Reindent installed?Song
I'm sure. It appear in the package control. I also already reinstalled it through package control.Rozamond
Could be related with - forum.sublimetext.com/t/…Heterogenetic
T
283

I have come across this issue too. I tried to figure it out by install jsFormat as follows:

  1. Open Package Control panel command+shift+p (mac), install package.

  2. Install jsFormat.

  3. Edit the shortcut for the jsformat: sublime text preferences, package settings, jsFormat

  4. Choose Key-Binding user, then edit

    [
      {
        "keys": [
          "command+l"
        ],
        "command": "reindent"
      },
      {
        "keys": [
          "ctrl+shift+s"
        ],
        "command": "auto_save"
      }, 
      {
        "keys": [
          "ctrl+alt+f"
        ],
        "command": "js_format"
      }
    ]
    
  5. So next time when you try to format json code, just use ctrl+option+f for mac. Not mac, just use ctrl+alt+f

That's all.

Triny answered 14/12, 2016 at 23:33 Comment(9)
I would like to upvote this a couple more times. Thanks y0.Rebba
On Linux it's CTRL + Shift +P and if you haven't already you need to select install package control first before running install package and then searching for jsformat.Therm
upvoted. Concise and helpful.FWIW on Mac this config is Ctrl-Option-FBacciform
ctrl + option + f in MacHeterodyne
Use this for control command j: "{ "keys": [ "ctrl+command+j" ], "command": "pretty_json" }"Darcidarcia
it is not available nowLovato
It was not working for me until i added "Key-binding user" configuration. After that works like a charm.Lutz
this is legit, works for MacOS 2022Balaam
This is a reference about how add User sublime3 setting. sublimetext.com/docs/settings.htmlIdeational
R
169

Install this packages via CMD + SHIFT + P > Install package > Search for Pretty JSON and install.

enter image description here

And then turn ugly json via CMD + CTRL + J (OSX) CTRL + ALT + J (Windows/Linux) in pretty JSON!

Source is on GitHub. Credit

Rhamnaceous answered 23/5, 2018 at 11:54 Comment(3)
This one was the answer for me!Anthe
Keep in mind you still need to set your key bindings for the shortcut as user Shubham Jain mentions below. CMD + CTRL + J does not work out of the box, at least for Sublime Text 3 and 4Knothole
Works excellent for me! thanksCamouflage
V
142

For sublime 4 below work for me on MAC OS :

Install "Pretty JSON" sublime 4 by referring below:

  • Use key combination Cmd + Shift + p and type Install Package
  • Select Package Control: Install Package from the search results
  • Type Pretty JSON and select it from the search results

OR use "Manual Installation" steps of below link

https://packagecontrol.io/packages/Pretty%20JSON


now for shortcut :

Click on Sublime -> Preferences -> Key Bindings

Here add the below json part inside existing json and save the file

{ "keys": [ "ctrl+shift+j" ], "command": "pretty_json" }

you can use cmd too instead of ctrl, set says as per your preference

Complete json will look like below:

[{
    "keys": [
      "command+l"
    ],
    "command": "reindent"
  },
  {
    "keys": [
      "ctrl+shift+s"
    ],
    "command": "auto_save"
  },
  {
    "keys": [
      "ctrl+shift+k"
    ],
    "command": "js_format"
  },
  {
    "keys": [
      "ctrl+shift+j"
    ],
    "command": "pretty_json"
  }
]

Now use below short key anytime to beautify json on sublime

ctrl+shift+j

You can also use command instead of ctrl

enter image description here

Venomous answered 15/7, 2021 at 5:51 Comment(0)
S
38

For Mac OS follow the steps:

Install Pretty JSON

  1. Use key combination Cmd + Shift + p and type Install Package
  2. Select Package Control: Install Package from the search results
  3. Type Pretty JSON and select it from the search results

Add Key Binding

  1. Open key bindings from Sublime Text > Preferences > Key Bindings

  2. Add following key short cut in key bindings

    { "keys": [ "command+shift+j" ], "command": "pretty_json" }

Use Pretty JSON

  1. Select JSON text and use key combination Cmd + Shift + j
Stepdaughter answered 2/1, 2022 at 22:34 Comment(0)
C
14

Sublime Text with JSON formatter

You can use Command Palette to format a text

  1. Install SublimePrettyJson

2.1 Format using Command Palette

//Call Command Palette
Ctrl+Shift+P

//Format code instead of  CMD + CTRL + J (OSX)
pretty format

2.2 Format using custom shortcut

Sublime Text -> Settings... -> Key Bindings

//{ "keys": [ "<custom_shortcut >" ], "command": "pretty_json" }
{ "keys": [ "ctrl+i" ], "command": "pretty_json" }

enter image description here

Codee answered 25/6, 2021 at 14:4 Comment(3)
As I know, you can bind to it a custom shortcut. See for example the accepted answer.Rozamond
Thats not correct, you can bind it .. I hve mentioned same in my answer in this threadVenomous
This answer should be removed, because in my question I explicitly ask for a shortcut, not using the command palette...Rozamond
S
12

If you look at the source, the name of the sublime_plugin.TextCommand subclass is SublimeJsonReindentCommand. Per the conventions for command names, the resulting command is named sublime_json_reindent. Simply change your key binding to:

{ "keys": ["ctrl+shift+j", "ctrl+shift+j"], "command": "sublime_json_reindent" },

and you should be all set.

Song answered 20/1, 2016 at 14:43 Comment(5)
The command does nothing. In the console nothing appears. If i put the command directly in the console: >>> sublime_json_reindent Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'sublime_json_reindent' is not definedRozamond
@Rozamond in the console, run view.run_command("sublime_json_reindent") and let me know what happens. Make sure you have a JSON file open and some text selected.Song
@Rozamond are you sure you have JSON Reindent installed?Song
I'm sure. It appear in the package control. I also already reinstalled it through package control.Rozamond
Could be related with - forum.sublimetext.com/t/…Heterogenetic
L
5

I am using Linux OS. I found the solution first installing the package controller then Pretty JSON package.

  1. Install the package controller for sublime here

  2. Install Pretty JSON packages via CMD + SHIFT + P > Install package > Search for Pretty JSON and install. Details are here.

Leptorrhine answered 19/5, 2017 at 18:59 Comment(0)
S
1

Since version 4 it is available under the Selection -> Format -> JSON menu option.

enter image description here

Som answered 2/5, 2022 at 20:37 Comment(5)
Is it a native feature or it still needs a package installed?Rozamond
It comes with it nowSom
it's not native, in Sublime 4216 there is no Format menu in SelectionClarissaclarisse
In version 4126 there is.Som
Yes, there is it now.Rozamond
L
0

Open the Command Palette: Press Ctrl+Shift+P (Windows) or Cmd+Shift+P (OS X). Type 'install' in the Command Palette input box, which should autocomplete to 'Install Package Control'. enter image description here

Then go to Sublime Text -> Preferences -> Package Control -> Type Package Control: Install Package in the pop up box which would open a separate pop up box and in that type the package you want to install in this case "Pretty JSON"

Turn ugly json to pretty JSON via CMD + CTRL + J (OS X)

Lectionary answered 17/12, 2021 at 0:20 Comment(2)
Hi, what's the purpose of your answer? "Pretty JSON" is already suggested in other answers here.Rozamond
I felt the some initial steps were missing and so I added this answer. I was struggling to install Pretty JSON since the answers did not explain how to install "Package Control" first. Only after installing Package Control you can install "Pretty JSON".Lectionary

© 2022 - 2024 — McMap. All rights reserved.