# \[space-lua\] Toggle / Rotate Header Level h1-h6-On/Off

**URL:** https://community.silverbullet.md/t/space-lua-toggle-rotate-header-level-h1-h6-on-off/3320
**Category:** Tricks & Techniques
**Created:** [October 2, 2025, 5:29pm UTC](https://community.silverbullet.md/t/space-lua-toggle-rotate-header-level-h1-h6-on-off/3320 "2025-10-02T17:29:08Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![Mr.Red](https://community.silverbullet.md/user_avatar/community.silverbullet.md/mr.red/32/821_2.png) [@Mr.Red](https://community.silverbullet.md/u/Mr.Red)
#### Post date: [October 3, 2025, 1:32pm UTC](https://community.silverbullet.md/t/space-lua-toggle-rotate-header-level-h1-h6-on-off/3320/4 "2025-10-03T13:32:18Z")

</div>

Here is an updated version of the Header: Toggle Level commands.  
This is derived from the “built-in” /h1-/h5 slash commands and added the part to handle the toggling off. More straightforward and still retains all the functions.

```auto
-- function to toggle a specific header level

local function toggleHeader(level)
  local line = editor.getCurrentLine()
  local text = line.textWithCursor

  -- Detect current header level
  local currentLevel = string.match(text, "^(#+)%s*")
  currentLevel = currentLevel and #currentLevel or 0

  local cleanText = string.gsub(text, "^#+%s*", "")

  -- Toggle: remove if same, otherwise set new level
  if currentLevel == level then
    editor.replaceRange(line.from, line.to, cleanText, true)
  else
    editor.replaceRange(line.from, line.to, string.rep("#", level) .. " " .. cleanText, true)
  end
end

-- register commands Ctrl-1 → Ctrl-6
for lvl = 1, 6 do
  command.define {
    name = "Header: Toggle Level " .. lvl,
    key = "Ctrl-" .. lvl,
    run = function() toggleHeader(lvl) end
  }
end

```

---

_[View the full topic](https://community.silverbullet.md/t/space-lua-toggle-rotate-header-level-h1-h6-on-off/3320)._
