Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable continuation of comments to the next line #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kasshedi117
Copy link

@kasshedi117 kasshedi117 commented Sep 17, 2023

vim.opt.formatoptions:remove("o") doesn't work, seems to be overrode by other plugin.

@sdsalyer
Copy link

sdsalyer commented Jun 2, 2024

EDIT:

Okay, you were right... and I think your PR is totally valid. Something was overwriting the format options. Credit to this guy here:

It was the same ftp plugin, I think referenced by Telescope, which is setting the format options, and I suppose is loaded after init.lua stuff.

If you want a lua-esque version, you can try this instead (note, this might not be the best/correct event to bind to, but it seems to work):

-- disable continuing comments on next line
vim.api.nvim_create_autocmd("BufEnter", {
    group = vim.api.nvim_create_augroup('format_options', {}),
    desc = 'Disable comment continuation on next line',
    pattern = '*',
    callback = function()
        vim.opt.formatoptions:remove({'c', 'r', 'o'})
    end
})

Explain:

There are 2 relevant examples in the Neovim docs

1.)

... working with list-like, map-like, and set-like options through Lua tables: Instead of

set wildignore=*.o,*.a,__pycache__
set listchars=space:_,tab:>~
set formatoptions=njt

you can use

vim.opt.wildignore = { '*.o', '*.a', '__pycache__' }
vim.opt.listchars = { space = '_', tab = '>~' }
vim.opt.formatoptions = { n = true, j = true, t = true }

2.)

These wrappers also come with methods that work similarly to their :set+=,
:set^= and :set-= counterparts in Vimscript:

vim.opt.shortmess:append({ I = true })
vim.opt.wildignore:prepend('*.o')
vim.opt.whichwrap:remove({ 'b', 's' })

The price to pay is that you cannot access the option values directly but must
use vim.opt:get():

You can also refer to the format options table for what the removal of these 3 options does:

fo-c

c Auto-wrap comments using 'textwidth', inserting the current comment
leader automatically.

fo-r

r Automatically insert the current comment leader after hitting
in Insert mode.

fo-o

o Automatically insert the current comment leader after hitting 'o' or
'O' in Normal mode. In case comment is unwanted in a specific place
use CTRL-U to quickly delete it. i_CTRL-U

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants