terrortylor / nvim-comment

A comment toggler for Neovim, written in Lua
MIT License
479 stars 23 forks source link

cannot change cpp comment format #43

Closed YanzhaoW closed 2 years ago

YanzhaoW commented 2 years ago

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.

zhang-stephen commented 2 years ago
local function nvim_comment()
    require('nvim_comment').setup()
    vim.api.nvim_command [[autocmd! FileType cpp :lua vim.api.nvim_buf_set_option(0, "commentstring", "// %s")]]
end

use {'terrortylor/nvim-comment', config = nvim_comment}

and it works well.