# JSON highlighters for keys

**URL:** <https://discuss.kakoune.com/t/json-highlighters-for-keys/758>\
**Category:** Help\
**Tags:** highlighter\
**Created:** [October 15, 2019, 1:51pm UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758 "2019-10-15T13:51:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delapouite](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/delapouite/32/10_2.png) [@Delapouite](https://discuss.kakoune.com/u/Delapouite)\
**Post date:** [October 15, 2019, 1:51pm UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/1 "2019-10-15T13:51:57Z")

</div>

Hi

Currently when I edit a JSON file, it looks like this. Almost everything is in yellow (which is my default color for strings):

 ![image](https://discuss.kakoune.com/uploads/default/original/1X/b5038f190e5d0e1fd997a3436dc045f57ab0cf2c.png)

I would love to have a different color for JSON keys (here in pink but it doesn’t matter):

 ![image](https://discuss.kakoune.com/uploads/default/original/1X/b5c1a965f8c74ebff37d056417410e392949a87a.png)

This way, I reckon it is way easier to spot string as values vs keys!

Here are the regions highlighters currently defined in [json.kak](https://github.com/mawww/kakoune/blob/master/rc/filetype/json.kak):

```auto
add-highlighter shared/json regions
add-highlighter shared/json/code default-region group
add-highlighter shared/json/string region '"' (?<!\\)(\\\\)*" fill string

add-highlighter shared/json/code/ regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value

```

I must admit that I’m very lame at writing regions delimiter and I failed to achieve what I depicted on the 2nd screenshot.

Does someone have the magic incantation for this?

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [October 17, 2019, 8:52am UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/2 "2019-10-17T08:52:56Z")

</div>

I gave it a shot. To me it seems it would be pretty difficult to handle JSON highlighting using regions in case such as this, so I ditched them and took regex only route:

```
add-highlighter shared/json group
add-highlighter shared/json/string regex (?<!\\)("(?:[^"]|\\")*(?<!\\)") 1:string
add-highlighter shared/json/key regex (?<!\\)("(?:[^"]|\\")*(?<!\\)")\s*: 1:green
add-highlighter shared/json/literal regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value

```

Wasn’t sure which face would be semantically best here, so left it at green.

---

<div class="post-metadata">

**Author:** ![Delapouite](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/delapouite/32/10_2.png) [@Delapouite](https://discuss.kakoune.com/u/Delapouite)\
**Post date:** [October 17, 2019, 9:46am UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/3 "2019-10-17T09:46:57Z")

</div>

Thanks a lot @cipharius ! Really appreciate your help.

I was about to try the `regex` road as well.

Here’s what it looks with your snippet:

 ![image](https://discuss.kakoune.com/uploads/default/original/1X/55491e1e72a9fd0c3d1c669bf87af15131936431.png)

Literals like numbers, `true` and `false` are hightlighted inside strings.

By moving the last hightlighter on first position, here’s the result:

 ![image](https://discuss.kakoune.com/uploads/default/original/1X/23dd83beb9ec3c20426892ef935be60d44194cfa.png)

I decided to use the `keyword` face for JSON keys, since it’s the one used by CSS properties, which are “quite” similar.

Finally, I put everything in a `ModuleLoaded` hook to overwrite the default highlighter provided by Kakoune:

```auto
hook global ModuleLoaded json %{
  remove-highlighter shared/json
  add-highlighter shared/json group
  add-highlighter shared/json/literal regex \b(true|false|null|\d+(?:\.\d+)?(?:[eE][+-]?\d*)?)\b 0:value
  add-highlighter shared/json/string regex (?<!\\)("(?:[^"]|\\")*(?<!\\)") 1:string
  add-highlighter shared/json/key regex (?<!\\)("(?:[^"]|\\")*(?<!\\)")\s*: 1:keyword
}

```

Do you think we should make a PR to change the current `json.kak`?

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [October 17, 2019, 12:14pm UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/4 "2019-10-17T12:14:00Z")

</div>

I thought about creating PR as well. Even if it doesn’t get accepted as is, I think it would still act as a useful thread for discussion.

If you don’t mind, I want to create the PR as I’m attending the Hacktoberfest event and I need few more PRs.

---

<div class="post-metadata">

**Author:** ![Delapouite](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/delapouite/32/10_2.png) [@Delapouite](https://discuss.kakoune.com/u/Delapouite)\
**Post date:** [October 17, 2019, 12:21pm UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/5 "2019-10-17T12:21:27Z")

</div>

Sure no problem! I let you create the PR.

---

<div class="post-metadata">

**Author:** ![cipharius](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/cipharius/32/184_2.png) [@cipharius](https://discuss.kakoune.com/u/cipharius)\
**Post date:** [October 17, 2019, 2:05pm UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/6 "2019-10-17T14:05:06Z")

</div>

Alright, I have started the PR here: [https://github.com/mawww/kakoune/pull/3142](https://github.com/mawww/kakoune/pull/3142)

---

<div class="post-metadata">

**Author:** ![gregk](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/g/958977/32.png) [@gregk](https://discuss.kakoune.com/u/gregk)\
**Post date:** [November 10, 2025, 7:28am UTC](https://discuss.kakoune.com/t/json-highlighters-for-keys/758/7 "2025-11-10T07:28:34Z")

</div>

Would also like a different colour for JSON keys! It looks like the recursive nature of JSON did in the PR. It looks to me that json.kak has not changed. Has there been any progress on this that I missed?

Meanwhile jq does a fine job highlighting JSON, nesting and all, in the terminal. However, the colours don’t survive piping jq into kak (unless jq is detecting which is at the other end of the pipe and going monochrome). Is there a way to make kakoune render terminal control characters (related to colour)?

This is not perfect either because it won’t correctly highlight any edits, but I find that I have to run JSON edits in kakoune through jq in any case to check for syntax errors, so perhaps it’s not that much of a drawback.
