"Toggle code" works like "Toggle Italic" and "Toggle Bold" + "Toggle Code Block"

Use case or problem

Basically, I made a bug report:

Steps to reproduce

Reproducible on Sandbox Vault

Active “Toggle Code”: `` appears
Write “some stuff”: `some stuff`
Active “Toggle Code”: `some stuff```

Expected result

Active “Toggle Code”: `some stuff` (cursor at the end)

I expect the same result that I have with “Toggle Bold” or “Toggle Italics”.

Active “Toggle Italics”: **
Write “some stuff”: *some stuff*
Active “Toggle Italics”: *some stuff* (cursor at the end)

Actual result

The cursor is in a middle of new ``, which is not intuitive.

Proposed solution

Make “Toggle code” works the same way as “Toggle Italic” and “Toggle Bold”.

Plus based on Angel’s answer on the bug report, this feature seems to work in MacOS. To be confirmed?

Additionnal feature “Toggle Code Block”

Well, I just have an idea on the way: it would be convenient to have also a “Toggle Code Block” option.

  • Active “Toggle Code Block”:
  • ```|
    ```

where | is the position of the cursor.

This “Toggle Code Block” could be implemented in two ways which are not exclusive:

  • Use “Toggle Code” twice in a row, very convenient if “Toggle Code” is already mapped to a hotkey.
  • Have a separate “Toggle Code Block” option that can be mapped with standard hotkey.

In addition it would be enclosed like “Toggle Italic” and “Toggle Bold”, namely when you are at the end of the code block then you can just “Toggle Code Block” to put the cursor out of the code block.

  • ```
    some code |
    ```
  • Use “Toggle Code Block”
  • ```
    some code
    ```|

where | is the position of the cursor.

Personal opinion

I do think the double use of “Toggle Code” can be convenient. Although, it would not be uniform with the rest of Obsidian: there is no format that needs to be activated twice in a row to have another format. From what I understand, Obsidian tries to keep things simple to have a reasonable software to maintain - which is very wise IMHO.

On the other side, I do think “Toggle Code Block” feature can be easily implemented and anyone could benefit without changing the UX.

Have a nice day,
AwePhD

3 Likes

Edit: Angel does not have the expected feature working on MacOS, here their reply on the bug report.

1 Like

So sorry for my confusion.

Angel

1 Like

Not sure I understand the main proposed solution, but came here for +1 to request “Toggle Code Block”

Frequently I just wanna select like 10 lines and say Ctrl+/ like in vscode to toggle a block of code. In obsidian this only puts single backticks, one on the first line, one on the last line - not proper syntax for any useful purpose.

Since “Toggle Code” is essentially broken for multiple lines, it would make sense to just change the behaviour to put triple backticks if the selection is multiple lines.

1 Like