# How to avoid redefining hotkeys?

**URL:** <https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763>\
**Category:** Help\
**Tags:** ui-ux, keyboard-shortcut\
**Created:** [November 16, 2022, 1:59pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763 "2022-11-16T13:59:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 16, 2022, 1:59pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/1 "2022-11-16T13:59:37Z")

</div>

### What I’m trying to do

I’m trying to find an available key to link some new (or old) functionality to a hotkey, but I don’t want to overwrite existing stuff unintentionally.

### Things I have tried

I’ve tried intentionally to overwrite a hotkey to see if I would get some warning, which I didn’t. Obsidian happily allowed me to assign cmd+P to multiple commands.

 ![image](https://forum.obsidian.md/uploads/default/original/3X/d/1/d1e4b6ab5238897c5b517f48528ddbc849504d70.png)

Tried searching for a command to list hotkeys, even tried doing the hotkey in the search filed of hotkeys. But no luck.

I did however find that Obsidian says it conflicts, but not any advise on how to avoid the conflicts, or finding the conflicts. The image below was captured when I hovered over the last definition.

 ![image](https://forum.obsidian.md/uploads/default/original/3X/e/c/ece7548c22fd55147103e75401271387d3862b2c.png)

I tried searching for resolving hotkey conflicts, which lead to some feature requests. One which is looping and is closed: [A way to find which command is bound a specific hotkey](https://forum.obsidian.md/t/a-way-to-see-what-functions-hotkeys-are-mapped-to/10119), and another one, [Sort hotkeys to the top](https://forum.obsidian.md/t/sort-hotkeys-to-the-top/27359), which doesn’t seem to get any more focus, or is that just me not knowing how to look for that?

So my current workaround would seem to be to hover over the hotkey, and if there is a conflict, I need to do a multi-line search in `.obsidian/hotkeys.json` and convert stuff like Cmd (or possibly Ctrl) to something like “Mod”, just to find a textual reference, which is not the same as in the Hotkeys settings, for the multiple definition of a given key. Kind of hard…

**In other words; Are there any progress on any features related to avoiding redefinition of hotkeys? Or are there better methods to resolve hotkey conflicts?**

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [November 16, 2022, 2:07pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/2 "2022-11-16T14:07:05Z")

</div>

Don’t conflicts show immediately in red?

 ![conflicts in red](https://forum.obsidian.md/uploads/default/original/3X/9/7/9754b8b26fd12a52c20709b47c7e280e852b3610.png)

Any use?

### Commands by hotkey

````auto
```dataviewjs

const getNestedObject = (nestedObj, pathArr) => {
    return pathArr.reduce((obj, key) =>
        (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj);
}

function hilite(keys, how) {
    // need to check if existing key combo is overridden by undefining it
    if (keys && keys[1][0] !== undefined) {
        return how + keys.flat(2).join(' ').replace('Mod', 'CMD').replace('Alt', 'OPT').replace('Ctrl', 'CTRL').replace('Shift', 'SHIFT').replace('Enter', 'ENTER') + how;
    } else {
        return how + '–' + how;
    }
}

function getHotkey(arr, highlight=true) {
    let hi = highlight ? '**' : '';
    let defkeys = arr.hotkeys ? [[getNestedObject(arr.hotkeys, [0, 'modifiers'])],
    [getNestedObject(arr.hotkeys, [0, 'key'])]] : undefined;
    let ck = app.hotkeyManager.customKeys[arr.id];
    var hotkeys = ck ? [[getNestedObject(ck, [0, 'modifiers'])], [getNestedObject(ck, [0, 'key'])]] : undefined;
    return hotkeys ? hilite(hotkeys, hi) : hilite(defkeys, '');
}

let cmds = dv.array(Object.entries(app.commands.commands))
    .where(v => getHotkey(v[1]) != '–')
    .sort(v => v[1].id, 'asc')
    .sort(v => getHotkey(v[1], false), 'asc');

dv.paragraph(cmds.length + " hotkeys. " +
    "User-defined hotkeys in <strong>bold</strong>.<br><br>");

dv.table(["Command", "Name", "Hotkeys"],
  cmds.map(v => [
    v[1].id,
    v[1].name,
    getHotkey(v[1]),
    ])
  );
```

````

### Commands by command name

````auto
```dataviewjs

const getNestedObject = (nestedObj, pathArr) => {
    return pathArr.reduce((obj, key) =>
        (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj);
}

function hilite(keys, how) {
    // need to check if existing key combo is overridden by undefining it
    if (keys && keys[1][0] !== undefined) {
        return how + keys.flat(2).join(' ').replace('Mod', 'CMD').replace('Alt', 'OPT').replace('Ctrl', 'CTRL').replace('Shift', 'SHIFT').replace('Enter', 'ENTER') + how;
    } else {
        return how + '–' + how;
    }
}

function getHotkey(arr, highlight=true) {
    let hi = highlight ? '**' : '';
    let defkeys = arr.hotkeys ? [[getNestedObject(arr.hotkeys, [0, 'modifiers'])],
    [getNestedObject(arr.hotkeys, [0, 'key'])]] : undefined;
    let ck = app.hotkeyManager.customKeys[arr.id];
    var hotkeys = ck ? [[getNestedObject(ck, [0, 'modifiers'])], [getNestedObject(ck, [0, 'key'])]] : undefined;
    return hotkeys ? hilite(hotkeys, hi) : hilite(defkeys, '');
}

let cmds = dv.array(Object.entries(app.commands.commands))
    .sort(v => v[1].id, 'asc');

dv.paragraph(cmds.length + " commands. " +
    "User-defined hotkeys in <strong>bold</strong>.<br><br>");

dv.table(["Command", "Name", "Hotkeys"],
  cmds.map(v => [
    v[1].id,
    v[1].name,
    getHotkey(v[1]),
    ])
  );
```

````

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 16, 2022, 2:34pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/3 "2022-11-16T14:34:43Z")

</div>

> [@anon63144152](#):
>
> Don’t conflicts show immediately in red?
> 
> ![conflicts in red](https://forum.obsidian.md/uploads/default/original/3X/9/7/9754b8b26fd12a52c20709b47c7e280e852b3610.png)

Kind of?  
 ![image](https://forum.obsidian.md/uploads/default/original/3X/a/a/aa9373d820ff27d4cd8edff4b4f4d4320b370f97.png)

On my screen that is not very clear, and it doesn’t show the conflicting setup.

> [@anon63144152](#):
>
> Any use?

If only it had shown those with either a conflicting key combination, and/or multiple key configurations. It seems to miss out on those cases. But other than that, it’s a nifty little script.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 16, 2022, 2:57pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/4 "2022-11-16T14:57:15Z")

</div>

Some questions related to @anon63144152 script, and potential issues, (or just me misreading your code):

> [@anon63144152](#):
>
> ````auto
> ```dataviewjs
> ...
> function getHotkey(arr, highlight=true) {
> let hi = highlight ? '**' : '';
> let defkeys = arr.hotkeys ? [[getNestedObject(arr.hotkeys, [0, 'modifiers'])],
> [getNestedObject(arr.hotkeys, [0, 'key'])]] : undefined;
> let ck = app.hotkeyManager.customKeys[arr.id];
> var hotkeys = ck ? [[getNestedObject(ck, [0, 'modifiers'])], [getNestedObject(ck, [0, 'key'])]] : undefined;
> return hotkeys ? hilite(hotkeys, hi) : hilite(defkeys, '');
> }
> 
> let cmds = dv.array(Object.entries(app.commands.commands))
> .where(v => getHotkey(v[1]) != '–')
> .sort(v => v[1].id, 'asc')
> .sort(v => getHotkey(v[1], false), 'asc');
> 
> dv.paragraph(cmds.length + " hotkeys. " +
> "User-defined hotkeys in <strong>bold</strong>.<br><br>");
> 
> dv.table(["Command", "Name", "Hotkeys"],
> cmds.map(v => [
> v[1].id,
> v[1].name,
> getHotkey(v[1]),
> ])
> );
> ```
> 
> ````

1. Could the missing cases of conflict multiple locations be related to the ternary return operation within `getHotkey` which _either_ returns a key from custom key bindings _or_ from default bindings, and thusly ignoring it might be in both?

2. Am I correct in assuming that `getHotkey()` is called three times for each command? Two in the `let cmds` part, and once in the `cmds.map` part?

3. Related to 2., is it possible to do a dataviewjs query using `as` clauses/aliases, and then do a sort related to that alias?

---

<div class="post-metadata">

**Author:** ![WhiteNoise](https://forum.obsidian.md/user_avatar/forum.obsidian.md/whitenoise/32/11_2.png) [@WhiteNoise](https://forum.obsidian.md/u/WhiteNoise)\
**Post date:** [November 16, 2022, 3:03pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/5 "2022-11-16T15:03:34Z")

</div>

I resurrected this FR [A way to find which command is bound a hotkey](https://forum.obsidian.md/t/a-way-to-see-what-functions-hotkeys-are-mapped-to/10119)

---

<div class="post-metadata">

**Author:** ![anon63144152](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@anon63144152](https://forum.obsidian.md/u/anon63144152)\
**Post date:** [November 16, 2022, 4:14pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/6 "2022-11-16T16:14:18Z")

</div>

I apologise for this answer in advance: a friend shared this code with me when they convinced me to switch from Scrivener to Obsidian. I just asked them where they got it from, and they said ‘the forum’ and sent this link:

> [@DataviewJS Snippet Showcase](https://forum.obsidian.md/t/dataviewjs-snippet-showcase/17847/37):
>
> Obsidian Command and Hotkey List (new version) In continuation of [the older post](https://forum.obsidian.md/t/dataviewjs-snippet-showcase/17847/13), here is a new version of the Obsidian Command List. Features: Commands sorted by assigned hotkey Commands sorted by internal Command ID Command names shown in the language Obsidian is currently set to—ideal for multi-lingual people who switch often, and for users of the [Buttons](https://github.com/shabegom/buttons), [Obsidian Leaflet](https://github.com/valentine195/obsidian-leaflet-plugin), [Command URI](https://github.com/deathau/command-uri-obsidian) and [Advanced Obsidian URI](https://github.com/Vinzent03/obsidian-advanced-uri) plugins. Now shows the current hotkey settings, not just the defaults. Modif…

I don’t know enough to be able to answer your questions but perhaps you can get some answers on the other thread.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [November 16, 2022, 6:38pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/7 "2022-11-16T18:38:14Z")

</div>

> [@anon63144152](#):
>
> I apologise for this answer in advance: a friend shared this code with me when they convinced me to switch from Scrivener to Obsidian. I just asked them where they got it from, and they said ‘the forum’ and sent this link:  
> …

Thanks @anon63144152 for making me aware of that link. Since I didn’t see the answers to my questions, I then proceeded to resolve most of them myself, and posted a reply on the other thread. See link below:

> [@DataviewJS Snippet Showcase](https://forum.obsidian.md/t/dataviewjs-snippet-showcase/17847/269):
>
> Last july, @Moonbase59 , created a script to list the current hotkeys, and I got news of this today. I discovered that if multiple keys was defined, it didn’t show the alternate keys provided, so I modified the scripts a little. Modifications I made to script include: Allowing for showing multiple keys for a given command Simplified the join operations on the modifiers, and allowed for usage of symbols, if one are so inclined Changed the second listing to only show commands without a hotkey, …

I’m also glad @WhiteNoise resurrected the FR, so that it might evolve into something which in the future would render these queries useless, as one could then do a search for say cmd + p.

Finally I found, that occasionally, and I don’t why or when, Obsidian does actually report where the conflicting hotkey is. Alas, it completely fails, when ~~some idiot~~ you assign the same key combination to more than two commands.

So, all in all, I reckon I’m good to go, and still hoping for the FR to be resolved.

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [November 23, 2022, 6:38pm UTC](https://forum.obsidian.md/t/how-to-avoid-redefining-hotkeys/47763/8 "2022-11-23T18:38:55Z")

</div>

Closed on November 23, 2022, 6:38pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
