I love the solution by @scwunch. I didn’t know the Templater plugin before, this is an amazing solution and opens up unlimited possibilities.
At the same time, I just discovered another easier solution leveraging jglev/obsidian-apply-patterns-plugin: An Obsidian plugin for applying patterns of find and replace in succession. (github.com).
Basically, the plugin allows us to apply customized regex to highlighted text. This is convenient for some tasks.
Here are the steps to promote/demote all selected headers.
- Install the plugin
- Copy below code to clipboard
- Click “Import patterns from clipboard” in
Apply Patterns
's option page.
Then enjoy the solution
- highlight any text range
- trigger “Apply Patterns: Apply patterns to selection”, you will see two commands “refactor: increase heading level” and “refactor: decrease heading level”
[
{
"name": "refactor: increase heading level",
"rules": [
{
"from": "^#",
"to": "##",
"caseInsensitive": false,
"global": true,
"multiline": true,
"sticky": false,
"disabled": false
}
],
"collapsed": false,
"cursorRegexStart": "$",
"cursorRegexEnd": "^"
},
{
"name": "refactor: decrease heading level",
"rules": [
{
"from": "^#",
"to": "",
"caseInsensitive": false,
"global": true,
"multiline": true,
"sticky": false,
"disabled": false
}
],
"collapsed": false,
"cursorRegexStart": "$",
"cursorRegexEnd": "^"
}
]