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

Update autocmd instructions based on new nvim api #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update autocmd instructions based on new nvim api
This commit updates the README with instructions for using Neovim's native API for setting autocmds.

The README updates should address issues like #57
  • Loading branch information
oneroyalace authored Oct 13, 2023
commit 79361a7a21a7e120d1deb1fa72d9db8a3edd5418
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,24 @@ vim.api.nvim_buf_set_option(0, "commentstring", "# %s")
```

You can also use an autocommand to automatically load your `commentstring` for
certain file types:
certain file types. For example, to load a `commentstring` for sql files,
in a config file, add:

```vim
" when you enter a (new) buffer
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
```lua
-- Creates an autocmd group for comment specifications
vim.api.nvim_create_augroup("comment", { clear = true })

-- Creates an autocmd that runs on BufEnter and BufFilePost
-- We use the `BufFilePost` trigger so that we can comment after changing file extensions
-- Without needing to repoen the buffer
vim.api.nvim_create_autocmd({"BufEnter", "BufFilePost"}, {
group = "comment",
pattern = {"*.sql"},
callback = function()
-- In SQL, lines are commented with "--"
vim.api.nvim_buf_set_option(0, "commentstring", "-- %s")
end
})
```

Or add the comment string option in the relevant `filetype` file:
Expand Down