# Simple word-count functionality

**URL:** https://community.silverbullet.md/t/simple-word-count-functionality/2159
**Category:** Tricks & Techniques
**Created:** [April 13, 2025, 3:14pm UTC](https://community.silverbullet.md/t/simple-word-count-functionality/2159 "2025-04-13T15:14:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![anthillsocial](https://community.silverbullet.md/user_avatar/community.silverbullet.md/anthillsocial/32/1137_2.png) [@anthillsocial](https://community.silverbullet.md/u/anthillsocial)
#### Post date: [April 13, 2025, 3:14pm UTC](https://community.silverbullet.md/t/simple-word-count-functionality/2159/1 "2025-04-13T15:14:42Z")

</div>

Here’s a space-lua script with some simple word-count functionality.

```space-lua
command.define {
  name = "Word Count",
  run = function()
    editor.flashNotification("Word count: "..countCurrentPage())
  end
}

function countCurrentPage()
  local text = editor.getText()
  return wordCount(text)
end

function wordCount(s)
  _,n = s:gsub("%S+","") -- count words
  return n
end

```

And some example expressions to write in your pages:

- Word count a string:  
${wordCount(“some random text”)}

- Simple button to return the word count for the current page:  
{[Word Count]}

- Display a wordcount for the current page:  
${countCurrentPage()}  
(though this isn’t accurate once text is edited)

Any improvements gratefully received. I should probably put these into their own namespace…

---

<div class="post-metadata">

### Author: ![anthillsocial](https://community.silverbullet.md/user_avatar/community.silverbullet.md/anthillsocial/32/1137_2.png) [@anthillsocial](https://community.silverbullet.md/u/anthillsocial)
#### Post date: [April 14, 2025, 8:07am UTC](https://community.silverbullet.md/t/simple-word-count-functionality/2159/2 "2025-04-14T08:07:46Z")

</div>

A little update to tidy things up into a namespace. I’m finding these small experiments to be a very useful way of getting my head around the SB ecosystem.

```space-lua
wc=wc or {}

function wc.currentpage()
  local text = editor.getText()
  return wc.string(text)
end

function wc.string(s)
  _,n = s:gsub("%S+","") -- count words
  return n
end

command.define {
  name = "Word Count",
  run = function()
    editor.flashNotification("Word count: "..wc.currentpage())
  end
}

```

**Expressions**  
${wc.string(“some random text”)}  
${wc.currentpage()}  
{[Word Count]}
