commit 9f76bdb411a561abf59057cac3139e1e454ba44a
parent 5e3a5e835260b2a436cad4cf0d8b0a1091a0f602
Author: Chris Bracken <chris@bracken.jp>
Date: Fri, 5 Jun 2026 22:07:17 +0900
nvim: soft-wrap markdown instead of hard wrapping
This looks a lot better when render-markdown is enabled and there are
significant differences in markdown length in characters vs the
rendererd length in characters. Links in particular really mess with
line wrapping.
Diffstat:
2 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/.config/nvim/after/ftplugin/markdown.lua b/.config/nvim/after/ftplugin/markdown.lua
@@ -1,6 +1,36 @@
+local utils = require("custom.utils")
+
+-- Hide colorcolumn.
+vim.opt_local.colorcolumn = ""
+
+-- Set tabs and indents.
vim.opt_local.tabstop = 2
vim.opt_local.shiftwidth = 2
vim.opt_local.softtabstop = 2
vim.opt_local.expandtab = true
-vim.opt_local.colorcolumn = "100"
-vim.opt_local.textwidth = 100
+
+-- Enable soft wrapping. Disable hard wrapping.
+vim.opt_local.wrap = true
+vim.opt_local.textwidth = 0
+vim.opt_local.wrapmargin = 0
+
+-- Wrap lines at word boundaries.
+vim.opt_local.linebreak = true
+
+-- Indent wrapped lines to match the start of the line; indent list item bodies.
+vim.opt_local.breakindent = true
+vim.opt_local.breakindentopt = "list:2"
+
+-- Prevent Neovim from auto-wrapping text as you type.
+vim.opt_local.formatoptions:remove("t")
+
+-- Since we're not hard-wrapping; remap common nav keys to move across physical
+-- lines instead of logical lines.
+vim.keymap.set({ 'n', 'v' }, 'j', 'gj', { buffer = true, silent = true })
+vim.keymap.set({ 'n', 'v' }, 'k', 'gk', { buffer = true, silent = true })
+vim.keymap.set({ 'n', 'v' }, '0', 'g0', { buffer = true, silent = true })
+vim.keymap.set({ 'n', 'v' }, '^', 'g^', { buffer = true, silent = true })
+vim.keymap.set({ 'n', 'v' }, '$', 'g$', { buffer = true, silent = true })
+
+-- Set the buffer width to 80 chars.
+utils.set_buffer_wrapwidth(80)
diff --git a/.config/nvim/lua/custom/utils.lua b/.config/nvim/lua/custom/utils.lua
@@ -55,4 +55,37 @@ function M.jump_daily(offset)
vim.cmd("edit " .. vim.fn.fnameescape(path))
end
+function M.set_buffer_wrapwidth(target_width)
+ local current_win = vim.api.nvim_get_current_win()
+ local width = vim.api.nvim_win_get_width(current_win)
+
+ -- Only split if your window is wider than target_width + 5
+ local threshold = target_width + 5
+ if width > threshold then
+ -- Create a vertical split on the right
+ vim.cmd("rightbelow vsplit")
+ local pad_win = vim.api.nvim_get_current_win()
+
+ -- Create a clean scratch buffer for the padding window
+ local pad_buf = vim.api.nvim_create_buf(false, true)
+ vim.api.nvim_win_set_buf(pad_win, pad_buf)
+
+ -- Hide line numbers, signs, and statuslines in the padding window
+ vim.wo[pad_win].number = false
+ vim.wo[pad_win].relativenumber = false
+ vim.wo[pad_win].signcolumn = "no"
+ vim.wo[pad_win].foldcolumn = "0"
+ vim.wo[pad_win].statuscolumn = ""
+ vim.bo[pad_buf].buftype = "nofile"
+ vim.bo[pad_buf].bufhidden = "wipe"
+
+ -- Move cursor back to the markdown window
+ vim.api.nvim_set_current_win(current_win)
+
+ -- Set markdown window width to target_width and lock it
+ vim.api.nvim_win_set_width(current_win, target_width)
+ vim.wo[current_win].winfixwidth = true
+ end
+end
+
return M