-
-
Notifications
You must be signed in to change notification settings - Fork 158
Examples
Lucas Pichette edited this page Jan 8, 2025
·
6 revisions
Bind a single key that selects between single and multiline comment styles based on the current context:
function _G.__toggle_contextual(vmode)
local cfg = require('Comment.config'):get()
local U = require('Comment.utils')
local Op = require('Comment.opfunc')
local range = U.get_region(vmode)
local same_line = range.srow == range.erow
local ctx = {
cmode = U.cmode.toggle,
range = range,
cmotion = U.cmotion[vmode] or U.cmotion.line,
ctype = same_line and U.ctype.linewise or U.ctype.blockwise,
}
local lcs, rcs = U.parse_cstr(cfg, ctx)
local lines = U.get_lines(range)
local params = {
range = range,
lines = lines,
cfg = cfg,
cmode = ctx.cmode,
lcs = lcs,
rcs = rcs,
}
if same_line then
Op.linewise(params)
else
Op.blockwise(params)
end
end
map('n', '<Leader>c', '<cmd>set operatorfunc=v:lua.__toggle_contextual<CR>g@')
map('x', '<Leader>c', '<cmd>set operatorfunc=v:lua.__toggle_contextual<CR>g@')
require('Comment').setup {
post_hook = function(ctx)
local r = unpack(vim.api.nvim_win_get_cursor(0))
local rcnt = vim.api.nvim_buf_line_count(0)
if rcnt > r then
vim.api.nvim_win_set_cursor(0, {r+1,0})
end
end,
}
Copies text to the clipboard before Comment.nvim modifies the lines. This is useful while debugging for referencing the previous version of the code.
pre_hook = function(ctx)
local lines = require("Comment.utils").get_lines(ctx.range)
vim.fn.setreg("+", lines)
end,
Below maps <leader>
+ /
to be a linewise comment if you're in normal mode, and a blockwise comment if you're in visual mode.
The visual-mode blockwise comment works for any visual selection, even inline words.
map("n", "<leader>/", function()
vim.api.nvim_feedkeys("gcc", "x", true)
end, { desc = "Toggle Line Comment" })
map("v", "<leader>/", function()
vim.api.nvim_feedkeys("gb", "v", true)
end, { desc = "Toggle Line Comment" })
Additional mappings for o
/a
/etc functionality with -prefix can be easily supported by making use of two keys after the key (as opposed to the singular /
) and referring to the Usage section of the README for what keys to feed in.