The Obsidian Code Block

A picture of a man sitting at a computer screen with code on it.

The humble Obsidian code block. You might think you need something special to use it, but that is incorrect: Obsidian supports code blocks out of the box.

Obsidian uses Markdown, which means that you can create code blocks using backticks. You can use a single backtick for inline code, `like this`, or three backtick “code fences”, like this:

```
This is a code block
```

This is supported by default in Obsidian, you can use it today!

But there’s a lot more you can do with an Obsidian code block. Let’s go through some more options.

Table of Contents

Folding Code Blocks

On occasion you may want to hide code in your Obsidian notes. If this is the case, you can do so by placing your code block in a collapsible callout.

The code for that looks a little weird. Here it is:

> [!NOTE]- Code Snippet
> ```
> Code goes here
> ```

I always have trouble remembering how to create callouts. I find it easiest to use the Command Palette (open it and search for “callout”), and then add the code block inside that.

In your note, that would show up like this:

A screenshot of an Obsidian callout with code inside it.

You can include as many callouts as you like, so you could create a whole library of code snippets this way. Callouts are also built-in to Obsidian, so you can use this without any setup.

Changing how the Obsidian Code Block Looks

Obsidian code blocks are affected by the theme you are using, but in my experience most themes don’t do much to style code blocks. They tend to look plain.

If you want to change how they look, there are numerous ways to do that. You could use a CSS Snippet, or—my preferred method—use a plugin.

The Style Settings plugin (open in Obsidian) is a community plugin that gives you the ability to change how Obsidian looks without writing any code. If you install the Style Settings plugin, and your theme supports it, you be able to modify how your Obsidian code blocks look.

Using the Minimal theme, here are the things you can adjust using Style Settings:

A screenshot of the Style Settings plugin in Obsidian, with it open to the Code Block settings.

There aren’t a ton of options by default: font size, scrolling, and colors. But it might be all you need. You can adjust the colors that are used when syntax highlighting, which is a nice feature.

Speaking of syntax highlighting, let’s talk about that next.

Syntax Highlighting in an Obsidian Code Block

Obsidian supports syntax highlighting by default. All you have to do is add the language to the top of your code block, like this:

```js
console.log('it works!');
```

Obsidian uses the Prism library, which currently supports nearly 300 different languages. See the full list of supported languages here.

If you need something more advanced, then check out the Editor Syntax Highlight Plugin (open in Obsidian). Most people won’t need this, but it’s worth a look if you need something that Prism doesn’t support out-of-the-box.

More Advanced Codeblocks

If you want even more from your Obsidian code blocks, you might want to look at the Code Styler plugin (open in Obsidian).

Code Styler adds a bunch of nice features to Obsidian, including:

  • Optional titles for code blocks
  • Line numbers
  • Allows you to use a different theme for code blocks than your main theme (see screenshot below)
  • Highlight lines of code
  • Supports collapsing by default—no callouts needed!
  • TONS of options to make codeblocks work exactly how you want

Code Styler makes code blocks a lot more functional and prettier, so if you use a lot of them, I’d recommend taking a look at it.

A screenshot of a fancy Obsidian code block using the Code Styler plugin.

Conclusion

Obsidian is a great place to track and reference blocks of code. I personally use it for all kinds of code snippets, some Obsidian related, others not.

If you want to start using Obsidian code blocks, I hope this article has given you a great place to start.

Do you have a great solution for Obsidian code blocks that I didn’t mention? Feel free to let me know in the comments below!

Leave a Reply

Your email address will not be published. Required fields are marked *