nanotee / nvim-lua-guide

A guide to using Lua in Neovim
5.83k stars 220 forks source link

neovim autocommands got merged, describe them in guide #100

Open matu3ba opened 2 years ago

matu3ba commented 2 years ago

https://github.com/neovim/neovim/pull/14661 (lua: autocmds take 2) see also the not yet merged PR on the docs: https://github.com/neovim/neovim/pull/17545/files

matu3ba commented 2 years ago

For something useful and minimal, you could provide

vim.api.nvim_create_augroup({ name = 'MYAUCMDS', clear = true })
-- text yank highlighting
vim.api.nvim_create_autocmd({ group='MYAUCMDS', event = 'TextYankPost', pattern = '*', command = [[silent! lua require'vim.highlight'.on_yank({timeout = 100})]], })
-- remove trailing spaces
vim.api.nvim_create_autocmd({ group='MYAUCMDS', event = 'BufWritePre', pattern = '*', command = [[:%s/\s\+$//e]] }) 
matu3ba commented 2 years ago

With the latest breaking change, this is now

vim.api.nvim_create_augroup('MYAUCMDS',  {clear = true})
vim.api.nvim_create_autocmd('TextYankPost', {group = 'MYAUCMDS', pattern = '*', command = [[silent! lua require'vim.highlight'.on_yank({timeout = 100})]]})
vim.api.nvim_create_autocmd('BufWritePre', {group = 'MYAUCMDS', pattern = '*', command = [[:%s/\s\+$//e]]}) -- remove trailing spaces
gegoune commented 2 years ago
vim.api.nvim_create_autocmd('TextYankPost', {group = 'MYAUCMDS', pattern = '*', command = [[silent! lua require'vim.highlight'.on_yank({timeout = 100})]]})

This one isn't correct although it works. You can use callback instead of `command:

vim.api.nvim_create_autocmd('TextYankPost', {group = 'MYAUCMDS', pattern = '*', callback = function() require'vim.highlight'.on_yank({timeout = 100}) end})

or even

vim.api.nvim_create_autocmd('TextYankPost', {group = 'MYAUCMDS', pattern = '*', callback = require'vim.highlight'.on_yank})

if there is no need to pass any arguments.

matu3ba commented 2 years ago

Also works for buffers now: https://github.com/neovim/neovim/pull/17594