"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

5 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.

3 Likes

For anybody looking for a temporary solution to this gap, the plugin Obsidian-shortcuts-extender has a “toggle code block” shortcut that works well enough.

Without an extension, you can at least add a code block easily. Just highlight the code and press the backtick key 3 times, which will surround your code with the needed 6 backticks and magically put them on newly inserted lines before/after your code. I didn’t expect it to work, but it does. There isn’t a built-in one-step method for reverting the code block back to regular text though, as far as I know.

2 Likes

ooh, highlight + typing backticks - that’s a nice trick!

The way I’ve been doing it (handy if you don’t want to select the code):

  • move mouse cursor to before your code
  • type three backticks (this inserts another line below with three closing backticks)
  • press Alt+Down until you move three backticks to the bottom of your code block

This is assuming you have a shortcut to move a line of text down as Alt+Down.

1 Like

You can’t un-code the coded text by selecting menu Format → Code.
But for highlight, Strikethrough and others, you have no problems.

this was implemented at some point.

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