A customisable neovim plugin to fix line widths and form paragraphs.
Neovim's textwidth is already
great! But, I find that gq
has some
shortcomings. This plugin will give you more control over paragraphs. Textangle does not
rely on the LSP (it is stand-alone). But, it can still reformat single-line code comments.
Neovim >= 0.5.0 is required.
With lazy.nvim,
{
"paulshuker/textangle.nvim",
-- Use only releases (optional).
version = "*",
}
In init.lua
, setup textangle.
require("textangle").setup({})
Then, format text on the cursor's line by typing :TextangleLine
.
You can also keymap it. For example, to gl
:
vim.api.nvim_set_keymap("n", "gl", "<cmd>TextangleLine<CR>", { noremap = true })
You can format text using visual line mode, I set it to keymap gk
:
vim.api.nvim_set_keymap(
"x",
"gk",
"<cmd>lua require('textangle').format_visual_line()<CR>",
{ noremap = true }
)
During setup, within the curly brackets, any default options can be overwritten. The default options are:
{
-- The maximum width of each line. When set to -1, Neovim's
-- [textwidth](https://neovim.io/doc/user/options.html#'textwidth') is used. See the
-- [editorconfig](https://neovim.io/doc/user/editorconfig.html) for ways to configure
-- textwidth project-wise.
line_width = -1,
-- Repeat the indent found on the first line on every line.
keep_indent = true,
-- If the first given line contains one of these prefixes (after any optional
-- indentation), then the prefix is repeated on every line. This is useful for
-- single-line comments. Whitespace must match too. Set to { } to disable.
keep_prefixes = { "-- ", "// ", "# " },
-- Allow words to be hyphenated. A word will be hyphenated if placing the entire word
-- on the next line leaves a whitespace greater than hyphenate_minimum_gap. The hyphen
-- is placed at the end of lines.
hyphenate = false,
-- See hyphenate.
hyphenate_minimum_gap = 10,
-- If a word is longer than line_width, hyphenate it. If false, words longer than
-- line_width will overflow.
hyphenate_overflow = true,
-- When disabled, textangle will silently do nothing whenever called.
disable = false,
}
You can call setup multiple times. Each time it is called, all previous options are forgotten. This way you can change the paragraph settings at any time.
If you just need to change the line_width based on file type, set max_line_length
in a
.editorconfig file in your working
directory and set textangle linewidth to -1 (default).
But, if you need finer tuning like hyphenation, use auto commands in your neovim config. For example,
-- Python files.
vim.api.nvim_create_autocmd({ "BufEnter" }, {
pattern = { "*.py" },
command = "lua require('textangle').setup({ line_width = 120, hyphenate = false })",
})
-- Text and lua files.
vim.api.nvim_create_autocmd({ "BufEnter" }, {
pattern = { "*.txt", "*.lua" },
command = "lua require('textangle').setup({ line_width = 100 })",
})
-- Do not format csv files.
vim.api.nvim_create_autocmd({ "BufEnter" }, {
pattern = { "*.csv" },
command = "lua require('textangle').setup({ disable = true })",
})
-- Add more file types here.
See autocmd for more details on auto commands, like changing formatting based on the file's full path.
Words are simply a series of letters/symbols given to textangle. If there were spaces, tabs or a new line in between the letters/symbols, then it would be considered two separate words. Hyphens already placed by the user in text are preserved.