Check out my latest book: Technical Writing for Software Engineers - A Handbook

Display the reading time, and words in NeoVim

Many writing tools include the very useful metrics of reading time and number of words. I thought it could be nice to have a similar thing in NeoVim.

I write two blogs in NeoVim (this one, and a tech blog), and I wanted to display the reading time, and number words for Markdown files. Here is the script I use. It’s worth noting that I use lualine for my status line management. Adapt to your needs according to what you use.

local function wordcount()
    return tostring(vim.fn.wordcount().words) .. ' words'
end

local function readingtime()
    return tostring(math.ceil(vim.fn.wordcount().words / 200.0)) .. ' min'
end

local function is_markdown()
    return vim.bo.filetype == "markdown" or vim.bo.filetype == "asciidoc"
end

require 'lualine'.setup {
    options = {
        icons_enabled = true,
        theme = 'onedark',
        -- component_separators = { left = 'ξ‚±', right = 'ξ‚³'},
        -- section_separators = { left = 'ξ‚°', right = 'ξ‚²'},
        component_separators = '',
        section_separators = '',
        disabled_filetypes = { 'vim-plug', 'Outline' },
        always_divide_middle = true,
        globalstatus = true,
    },
    sections = {
        lualine_a = { 'mode' },
        lualine_b = { { 'branch', icon = 'ξ‚ ', color = { fg = c.cyan } }, 'diff' },
        lualine_c = {
            {
                'filename',
                path = 0,
                symbols = { modified = ' ο‘ˆ', readonly = ' ο‘–', unnamed = '[No Name]', newfile = '[New]' }
            } },
        lualine_x = { lsp, 'diagnostics', { 'filetype', colored = true, icon_only = false },
            { wordcount,   cond = is_markdown },
            { readingtime, cond = is_markdown },
            'encoding',
            { 'fileformat', icons_enabled = false } },
        lualine_y = { 'progress' },
        lualine_z = { 'location' }
    },
    inactive_sections = {
        lualine_a = {},
        lualine_b = {},
        lualine_c = { 'filename' },
        lualine_x = { 'location' },
        lualine_y = {},
        lualine_z = {}
    },
    winbar = {
        lualine_a = {},
        lualine_b = {},
        lualine_c = { { 'filename', path = 1 }, { navic.get_location, cond = navic.is_available }, },
        lualine_x = {},
        lualine_y = {},
        lualine_z = {}
    },
    inactive_winbar = {
        lualine_a = {},
        lualine_b = {},
        lualine_c = { { 'filename', path = 1 }, { navic.get_location, cond = navic.is_available }, },
        lualine_x = {},
        lualine_y = {},
        lualine_z = {}
    },
    tabline = {},
    extensions = { 'fzf', 'nvim-tree', 'nvim-dap-ui' }
}

The important parts are:

{ wordcount,   cond = is_markdown },
{ readingtime, cond = is_markdown },

Which means words count, and reading time will be displayed only for markdown and asciidoc file types.

The entire code for the lualine can be found in my GitHub

lua

Share this article