Lekha is a minimalist (in appearance and function) NeoVim plugin for novel
writing:
- It expects the work to be all in one file.
- Top level headings (Lines that start with
#) are interpreted as chapters. - It periodically computes and displays word counts for chapters and the whole
manuscript. Commented text (
<!-- x y z -->) is not included in the word count. - Lines that contain a string like
TODO:somethingappear in a list of TODOs for each chapter. Thesomethinghas to be a single word. Intended use is to add aTODOin a comment block with details of what TODO. - It uses NeoVim's native command auto-complete feature to display chapters,
chapter word counts and TODOs when the
LekhaGotocommand is called (Please see screenshot above). - There is no split pane. This reduces visual clutter while keeping the information accessible.
git clone the repository under .config/nvim/pack/bengal-bionics/start
The bengal-bionics part is just a name I like, you can call it anything. This
gets you the Lekha Lua plugin, the Lekha colorschemes) and the
markdown ftplugin
If you just want the Lua plugin, copy the lua folder to somewhere in your
runtime path. Then add something like this to your markdown.vim:
-- Register Lekha commands
lua require("lekha").enable()
-- Register a convenient shortcut key
nmap <Tab> :LekhaGoto<Space>
Now by hitting Tab twice in normal mode you can access the LekhaGoto command
and NeoVim's auto-complete will list chapters.
Try it out with the example document!
Add %{%v:lua.require'lekha'.status_line()%} to the status line to print useful
information, like chapter name, chapter wordcount and document word count.
NeoVim has a built in wordcount() function, but it must be redoing the
wordcount at each call: I found it made scrolling on NeoVim unusably slow and
stuttery for documents with > 100,000 words.
lekha only recomputes things when we stop typing (CursorHold/CursorHoldI) so
it doesn't interrupt our flow. lekha has a document word count property which is
used in the status_line function.
Lekha supplies two other commands LekhaGotoChapter and LekhaGotoTodo where
the auto-completion shows only chapters or TODOs respectively.
Code formatting using stylua. I just use the binary release at https://github.com/JohnnyMorganz/StyLua/releases
Test using:
lua lekha_test.lua -v
- I had little trouble writing this NeoVim plugin in Lua though I had never written a NeoVim plugin before and didn't know Lua. The NeoVim documentation is adequate though I had to supplement with web searches for a few things.
- Vim/NeoVim's editing efficiency impresses me. I have a 140,000 word manuscript and Vim/NeoVim handles it without any issue. I used VS Code for a long time but when I tried editing the manuscript with it, the simple task of word wrapping (which needs a plugin on VS Code) broke in a subtle way making it unusable.
- I used an O(N) algorithm (N = manuscript length). It takes ~20ms to process a 140,000 word (~800 kB)text, which is decently fast for an interpreted, studiously dynamic language.
- Ordered arrays
- Calling NeoVim commands from Lua
- Creating autocommands and commands
- Unit testing with LuaUnit
- outline.nvim is both way more powerful and not powerful enough for my writing needs. It has a split-pane outline view, but not per-chapter word count and no TODO/bookmark feature.
- minimal-bookmarks.nvim
- section-wordcount.nvim
