neovim-writer-config/sentence_lines.lua
2025-09-25 01:09:18 -04:00

23 lines
774 B
Lua

local function split_sentences_paragraph()
-- save cursor and window view
local view = vim.fn.winsaveview()
-- find the paragraph boundaries
local start_line = vim.fn.search('^\\s*$', 'bnW') + 1
if start_line == 1 then start_line = 1 end
local end_line = vim.fn.search('^\\s*$', 'nW') - 1
if end_line < start_line then end_line = vim.fn.line('$') end
-- build a range and run substitute with 'e' flag
local cmd = string.format("%d,%ds/\\([.?!]\\)\\s\\+/\\1\r/ge", start_line, end_line)
vim.cmd(cmd)
-- restore cursor
vim.fn.winrestview(view)
end
-- run after leaving insert mode
vim.api.nvim_create_autocmd({ "InsertLeave", "TextChanged" }, {
pattern = { "*.md", "*.txt" },
callback = split_sentences_paragraph
})