terrortylor / nvim-comment Goto Github PK
View Code? Open in Web Editor NEWA comment toggler for Neovim, written in Lua
License: MIT License
A comment toggler for Neovim, written in Lua
License: MIT License
Shows commentstring not understood.
I use the following to configure nvim-comment. After I updated today my mappings broke. I think it was caused by this commit
22f81f9
require('nvim_comment').setup({
marker_padding = true,
comment_empty = true
create_mappings = true
line_mapping = "<leader>cc",zzzzzzz
operator_mapping = "<leader>c"
})
I am looking that something tpope commentary plugin has where I can do 4gcc
which would comment current line plus 3 more. I see there is gc4j
. Maybe there is a way I could do this already and I just need to create the map myself
Hi, adding a single line comments to htmldjango files was straight forward as it was explained in the docs. However, I couldn't find something to add block comment marks.
require'nvim_comment'.setup(
{
hook = function()
if vim.api.nvim_buf_get_option(0, "filetype") == "htmldjango" then
-- Assumes this is being run in the context of the filetype...
vim.api.nvim_buf_set_option(0, "commentstring", "{# %s #}")
end
end
}
)
{% comment %}
<div>
<p>foo</p>
</div>
{% endcomment %}
This is an example how a block comment should look like. If you point me towards the proper position in the source code, I could try adding it and creating a PR?
Hi, is it possible to comment with different comment strings based on cursor position in multi-context files like those of Vue and React? There is Shougo/context_filetype.vim which could help with figuring out the current context, but not sure how to make it work with nvim-comment
.
When using the plugin inside .nix
files, the inserted comment string is /* ... */
while it should be #
.
Is it actually the job of this plugin to handle this ? Are the default commentstring
values defined elsewhere ?
Thank you for your consideration.
Hey again.
I have used tcomment for a long time and gotten used to <C-_><C-_>
mapping. So I did set line_mapping
to it
line_mapping = "<C-_><C-_>"
it works fine except for visual mode. tcomment did support that, I know there is operator_mapping
but I would like to keep that set to default value.
Do you think I could somehow map <C-_><C-_>
to toggle single line in normal mode and selection in visual mode too?
Thanks!
It seems you can't use the same operator for normal and visual mode.
Can you implement that?
Iām using padded and unpadded comments. Having a way to create mappings for each would be useful. Ideas:
line_mapping_padded
and line_mapping_unpadded
marker_padding
parameter that can be called in custom mappingsWhen commenting lines in a zig file the comment style used is /* ... */
but zig comments should use // ...
.
Hey,
currently line commenting(gcc) comments line starting with // and /* / for block commenting, I like to have / */ commenting for one line comments as well, it's also the default behavior with tpope's commentary plugin
I could do autocmd FileType html.handlebars setlocal commentstring={{!--\ %s\ --}}
for kommentary plugin
Is there a way to configure nvim-comment for handlebars?
Not all languages support this but some like C/C++/Rust support block comments that would allow you to comment text at a finer granularity than lines.
Is there any interest/work on supporting this?
In C++, I prefer //
over /* */
.
How can I use line comment instead of block comment?
Hi! I have the following nvim_comment setup:
-- Code commenting
require("nvim_comment").setup()
My full config at: https://github.com/maciejzj/dotfiles/blob/master/init.lua
I expect the default setup from nvim-comment's README to include:
-- text object mapping, comment chunk,,
comment_chunk_text_object = "ic",
The comment text objects seem not to work (at least in bash scripts and python).
When I edit a chunk of text like this:
# Matplotlib components are oververbose < Cursor on this line
matplotlib.pyplot.set_loglevel('info')
And type cic
in normal mode I would expect to change inside comment, yet nothing really happens (nvim enters insert mode).
Versions:
NVIM v0.8.3
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@Ventura-arm64.
nim-comment@e9ac16ab056695cad6461173693069ec070d2b23
I have comment_empty_trim_whitespace = true
but doing a simple comment/uncomment on something like this leaves the middle line with spaces when only the inner block is selected
if true then
local foo = 1 -- visual mode from here
foo = foo + 1 -- to here
end
If I select the whole top level statement it works, but if I comment out the middle part it does not, and leaves as many spaces as it was initially indented. This is undesirable, because at least in my experience one does not want whitespace on empty lines in indented code.
I dug into the code a bit and found that the pattern is matched, but the replacement seems wrong
function M.uncomment_line(l, left, right, comment_empty_trim_whitespace)
local line = l
if right and right ~= "" then
line = line:gsub(vim.pesc(right) .. "$", "")
return line:gsub(vim.pesc(left), "", 1)
end
if comment_empty_trim_whitespace and left:match("%s+$") then
local left_nw = left:match("^(%S+)%s+$")
if line:match("^%s*" .. left_nw .. "$") then
print("THIS GETS PRINTED EXACTLY ONCE, BUT NO REPLACEMENT")
return line:gsub(vim.pesc(left_nw), "", 1)
end
end
return line:gsub(vim.pesc(left), "", 1)
end
Replacing
return line:gsub(vim.pesc(left_nw), "", 1)
with
return line:gsub("^%s*" .. vim.pesc(left_nw) .. "%s*", "", 1)
fixes it for me. I don't know much about neovim's APIs, so not sure if this is the best way to do it, but it does seem to work. Testing it even on some bigger code seems to produce the expected results.
while tpope/vim-commentary still works properly in the same situation
Currently this plugin doesn't work for visual mode. It will only comment out the last selected line.
local foo = 'foo'
local bar = 'bar'
local baz = 'baz'
local foo = 'foo'
local bar = 'bar'
local baz = 'baz'
gc
u
gc
Only the first three lines will be commented out after the above steps when all 6 lines should be commented out.
Neovim 0.10 introduced built-in commenting support:
Does it make this plugin redundant? If not, the differences should be mentioned in the readme.
Anything with a comment that has no indent is not toggled, like this for example:
--local test = 0
Actual:
-- --local test = 0
Expected:
local test = 0
This also happens with any language with a comment that has no indent.
Thank you very much for your great plugin!
One thing I'm missing from tpope's plugin is how it solved nested comments. This is especially relevant for the html comments that I use in my markdown files: <!-- comment -->
. The problem occurs when you have a text such as:
This is a<!-- comment --> text.
When uncommenting this it becomes:
<!--This is a<!-- comment --> text.-->
That's unexpected because the comment that begins at the beginning of the line now ends at the first -->
.
Tpope's plugin solved that by converting the nested comments into <1!-- comment --1>
. The text then becomes:
<!--This is a<1!-- comment --1> text.-->
This string could then of course be uncommented again as well. I'm not sure if this affects languages other than markdown.
If you think this is something that nvim-comment
should know how to do, you might also want to take a look at this issue; apparently the way tpope solved it leads to some problems (though this has never affected me).
Hi, do you think commenting, uncommenting or toggling could be made .
repeatable?
I tried to assign the same mapping to line_mapping
and operator_mapping
, like with vim-commentary, but unfortunately this doesn't work:
require('nvim_comment').setup({
-- Should key mappings be created
create_mappings = true,
-- Normal mode mapping left hand side
line_mapping = '<leader>c',
-- Visual/Operator mapping left hand side
operator_mapping = '<leader>c'
})
Am I missing something or is this (currently) not possible with nvim-comment out of the box?
Visually select and :CommentToggle
over the following python test case:
def edgar(x):
print("Once upon a midnight dreary")
Actual:
# def edgar(x):
# print("Once upon a midnight dreary")
Expected:
# def edgar(x):
# print("Once upon a midnight dreary")
e.g. uncommenting this will work
-- this is
-- a lua comment
But uncommenting this will not do anything:
--this is
--a lua comment
NVIM v0.5.0
Build type: Release
LuaJIT 2.1.0-beta3
File Type: Go
Before I run format source(go fmt), everything is OK. But as soon as the source is formatted, the toggling doesn't work for range operation, but works for a single line.
The go fmt will change the source format.
Would it be possible to add an option to allow inserting comment characters at the level of indentation?
I'm moving from tpope's vim-commentary plugin and this behavior is actually default, but he offers an option to make it work like nvim-comment does now:
Assign b:commentary_startofline to insert comment characters at column 1 regardless of indentation.
Need other configs?
require('nvim_comment').setup({
-- Linters prefer comment and line to have a space in between markers
marker_padding = true,
-- should comment out empty or whitespace only lines
comment_empty = true,
-- trim empty comment whitespace
comment_empty_trim_whitespace = true,
-- Should key mappings be created
create_mappings = true,
-- Normal mode mapping left hand side
line_mapping = "gcc",
-- Visual/Operator mapping left hand side
operator_mapping = "gc",
-- text object mapping, comment chunk,,
comment_chunk_text_object = "ic",
-- Hook function to call before commenting takes place
hook = nil
})
After toggling comments for a paragraph (or in any visual mode), I'd like to have some way of returning my cursor to the previous location. Currently, executing a comment toggle with either gcip
or vip:CommentToggle<cr>
appears to place the cursor at the beginning of the commented block and erase all marks within the commented block.
Here's how I can recreate this.
In my init.vim
, I have the following lines.
" ff: toggle comment for current line
nnoremap <silent> ff :CommentToggle<cr>
I can place my cursor on the first "n" in "nnoremap" and create a mark by running mt
. I can check that my mark exists my running :marks
. I see something like so.
:marks
mark line col file/text
mark line col file/text
' 321 0 " ff: toggle comment for current line.
l 321 0 " ff: toggle comment for current line.
p 328 0 -invalid-
t 322 10 nnoremap <silent> ff :CommentToggle<cr>
y 325 10 nnoremap <silent> fp mtvip:CommentToggle<cr>`t
I can then comment these two lines by running gcip
. I can then run :marks
again, and see that the t
mark is no longer present.
:marks
mark line col file/text
p 328 0 -invalid-
y 325 10 nnoremap <silent> fp mtvip:CommentToggle<cr>`t
Note that undoing the comment also recreates the mark.
So finding a way to preserve marks after block commenting would do the trick, because then I could do something like
mtvip:CommentToggle<cr>`t
to comment a paragraph and go back to my t
mark.
Thanks.
Hi,
I want to change the comment format of cpp file from "/*...*/" to "//...". According to README, I wrote following codes in my config file (.lua file):
vim.cmd([[
augroup usergroup
autocmd!
autocmd FileType cpp :lua vim.api.nvim_buf_set_option(0, "commentstring", "// %s")
augroup END
]])
But it didn't work at all. The comment format is still "/*...*/"
Also I don't want to do
augroup set-commentstring-ag
autocmd!
autocmd BufEnter *.cpp,*.h :lua vim.api.nvim_buf_set_option(0, "commentstring", "// %s")
" when you've changed the name of a file opened in a buffer, the file type may have changed
autocmd BufFilePost *.cpp,*.h :lua vim.api.nvim_buf_set_option(0, "commentstring", "// %s")
augroup END
as is shown in README file because cpp files can have many extensions, such as .cxx .cc .cpp or .h .hpp .hh etc.
So I would like to set commentstring based on filetype instead of file extension.
Thanks for this nice plugin. It surely can replace tcomment
which I am currently using.
While using this plugin, I found a weird behavior while commenting. So, If some block of code has an empty line then the commenting doesn't exclude the tab width. The behavior is clearly shown in the video below.
As you can see, the function below has some empty lines thus the weird commenting is happening. But the above function doesn't have empty lines, so the commenting is as expected. I also checked this behavior in typescript and this issue is also there.
For instance, for comments in Markdown,
<!-- actual-->
<!-- expected -->
(actual
is missing a space to the right.)
Hi, I am learning the code of nvim-comment, what is the use of saving @@
?
nvim-comment/lua/nvim_comment.lua
Line 197 in e9ac16a
Please help!!!
When I have cursor in commented line (lua script), after pressing enter - the comment is automatically duplicated to new line like so:
Does not happen for python. Did not test with other languages.
If it could be made consistent between nanguages, and option to disable it pernamently (if it is not bug)
I thought comment_empty
would enable this behaviour but it doesn't seem to do so.
In the below example, highlighting all 3 lines and running :CommentToggle
has the expected result of commenting each line. If you put your cursor on line 2 (empty), however, and run :CommentToggle
, no comment is created.
1 local pitaya = 'fruit'
2
3 local carrot = 'veggie'
It would be lovely if we were able to comment a single empty line, eg. for starting a new comment instead of commenting existing text š„°
This is a feature of commentary.vim that I use a lot, and I think it would be a good addition, at least as an option.
toml files seem to use /* */
pattern when the should use #
A declarative, efficient, and flexible JavaScript library for building user interfaces.
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ššš
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ā¤ļø Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.