Smart Code Blocks

pending

by Nathan Sujatno

Enhances code blocks to be more like an IDE.

2 starsUpdated 2mo ago0BSDDiscovered via Obsidian Unofficial Plugins
View on GitHub

Smart Code Blocks

Smart Code Blocks is an Obsidian plugin designed to significantly enhance the code editing experience within Obsidian. It brings IDE-like features to your code blocks, making it easier to write and format code without leaving your notes.

Features

1. Smart Enter

Automatically handles indentation when you press Enter keeping your code clean and structured.

  • Split and Indent: Pressing Enter between braces {} or parentheses () automatically creates a new line with proper indentation and places the closing character on the next line.
  • Indent after Colon: Pressing Enter after a colon : (e.g., in Python or text) automatically indents the next line.
  • Auto-Indent: Preserves the current indentation level for new lines.

2. Smart Tab

Stops the Tab key from moving focus out of the code block, allowing for proper code indentation.

  • Block Indentation: Pressing Tab inside a code block indents the code (inserting spaces or tabs based on settings), just like in a full IDE.
  • Empty Line Handling: Works seamlessly on empty lines to maintain structure.

3. Auto Pairs

Automatically closes common bracket and quote pairs to speed up typing and reduce errors.

  • Feature Pairs: Automatically closes (), [], {}, "", '', and backticks.
  • Triple Quotes: Supports """ and ''' for multi-line strings.
  • Block Comments: Typing /* automatically inserts */.

4. Smart Commenting

Easily toggle line comments within your code blocks.

  • Language Detection: Automatically detects the language of the code block (e.g., Python, JavaScript, SQL) to apply the correct comment syntax.
  • Toggle Command: Set toggle command in settings to Ctrl + / (or Cmd + / on Mac) to comment or uncomment the current line or selection.
  • Space Handling: Works correctly even with spaced language definitions (e.g., ``` Python).

5. Tab Out

Intuitively jump over closing characters to improve typing flow.

  • Seamless Navigation: Pressing Tab immediately before a closing character (), ], }, ", ') moves the cursor past it instead of inserting whitespace.
  • Smart Context: Handles nested references, triple quotes, and ignores escaped quotes.
  • Strict Whitespace: Preserves standard indentation behavior if there is whitespace between the cursor and the closing character.

6. Line Operations

Perform common line manipulation tasks directly within code blocks.

  • Move Line Up/Down: Quickly reorder lines of code using commands.
  • Duplicate Line: Instantly duplicate the current line.

7. Multi-Cursor / Selection

Simultaneously edit multiple occurrences of text.

  • Select Next Occurrence: Adds a new cursor to the next occurrence of the currently selected text.
  • Word Selection: If nothing is selected, it first selects the current word.

Recommended Hotkeys

To fully utilize these features, we recommend assigning hotkeys in Obsidian settings. You can do this manually, or simply go to Settings > Smart Code Blocks and click "Apply hotkeys" to set them all at once.

  • Toggle Line Comment: Ctrl + / (or Cmd + /)
  • Move Line Up: Alt + Up Arrow
  • Move Line Down: Alt + Down Arrow
  • Move Line Down: Alt + Down Arrow
  • Duplicate Line: Shift + Alt + Down Arrow
  • Select Next Occurrence: Ctrl + D (or Cmd + D)

Installation

  1. Copy the main.js, styles.css, and manifest.json files to your vault's plugin folder: <VaultFolder>/.obsidian/plugins/smart-code-blocks/.
  2. Reload Obsidian.
  3. Enable "Smart Code Blocks" in the Community Plugins settings.

License

MIT

Acknowledgements

This plugin was built using the Obsidian Sample Plugin as a foundation.

Special thanks to the Obsidian team and community for providing the initial structure and documentation.

For plugin developers

Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.