Always single line, comment sensitive, indentation preserving commenting.
Turn on notifications in Breaking Changes if using this plugin.
- Features
- Installation
- Keybindings
- Lazy load it Quickstart
-
Simple and stupid, just complain and i will change some lua tables around and we are ready to work
-
Supports
- counts like 5{comment} and dotrepeat
- commenting in front of the current line, and start a comment on empty lines
- toggling a comment in front/top of the current line, preview
- pasting as a comment, accounting for indentation
- block comments in case you really need them, and removing innermost block on cursor, preview
-
Simplest of them all ~270 loc in a single file
-
It uses nvim-ts-context-commentstring automatically if available them tries to turn all results into line comments
-
Single line comments avoid unexpected results when commenting:
- uncomments only when all the text selected is commented, avoiding confusion when getting big blocks of code out of the way for debugging
- always comments at the most shallow comment to make maintaining different levels of commented code easier
- when you have block comments at the end of lines other plugins fail on the simple task of getting this line out of the way, this plugin should never fail
use {
"lucastavaresa/SingleComment.nvim",
}
{
"lucastavaresa/SingleComment.nvim",
}
There is no keybindings by default.
Those are all the available functions:
-- comments the current line, or a number of lines 5gcc
vim.keymap.set("n", "gcc", require("SingleComment").SingleComment, { expr = true })
-- comments the selected lines
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
-- toggle a comment top/ahead of the current line
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
-- comments ahead of the current line
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
-- comment a block, and removes the innermost block comment in normal mode
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
-- pastes as a comment, accounting for indentation
vim.keymap.set({ "n", "v" }, "gcp", require("SingleComment").CommentPaste)
Get a table with comment beginning and end
Improved by ts-context-commentstring but also works without it automatically, them gets single-lined/tweaked by this plugin custom tables
Useful for custom utility functions that need accurate comment detection
-- you can pass "block", and it will **try** to get block comments
local comment = require("SingleComment").GetComment()
Those commands substitute all the above
- with packer i did not test this, please inform me if it works :)
use {
"lucastavaresa/SingleComment.nvim",
opt = true,
keybindings = {
{ { "n", "v" }, "gcc" }, { "n", "gca" }, { "n", "gcA" },
{ { "n", "v" }, "gcb" }, { { "n", "v" }, "gcp" }
},
requires = {
"nvim-treesitter/nvim-treesitter",
"JoosepAlviste/nvim-ts-context-commentstring"
},
setup = function()
vim.keymap.set(
"n",
"gcc",
require("SingleComment").SingleComment,
{ expr = true }
)
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
vim.keymap.set({ "n", "v" }, "gcp", require("SingleComment").CommentPaste)
end
}
- with lazy:
{
"lucastavaresa/SingleComment.nvim",
lazy = true,
dependencies = {
"nvim-treesitter/nvim-treesitter",
"JoosepAlviste/nvim-ts-context-commentstring"
},
init = function()
vim.keymap.set("n", "gcc", require("SingleComment").SingleComment, { expr = true })
vim.keymap.set("v", "gcc", require("SingleComment").Comment, {})
vim.keymap.set("n", "gca", require("SingleComment").ToggleCommentAhead, {})
vim.keymap.set("n", "gcA", require("SingleComment").CommentAhead, {})
vim.keymap.set({ "n", "v" }, "gcb", require("SingleComment").BlockComment)
vim.keymap.set({ "n", "v" }, "gcp", require("SingleComment").CommentPaste)
end,
},