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

README: improve autocmd instructions #1082

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

Conversation

amarakon
Copy link

@amarakon amarakon commented Oct 2, 2022

First, you can use source without arguments to source the current buffer. As for Lua, it is better to use actual Lua code than calling Vim from Lua.

@AzP
Copy link

AzP commented Nov 11, 2022

I was just about to supply a PR with this change, good I found that you'd already done it :)

Copy link

@AzP AzP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this and it works well, and is the more Lua way of doing it in nvim.

@dsully
Copy link
Contributor

dsully commented Nov 13, 2022

Even more lua:

vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = "lua/plugins.lua",
  callback = function(args)
    vim.cmd.source(args.file)
    vim.cmd.PackerCompile()
  end,
  desc = "Compile Packer plugins source.",
})

@AzP
Copy link

AzP commented Nov 13, 2022

Nice, looks even better.

@Susensio
Copy link

Even more lua:

vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = "lua/plugins.lua",
  callback = function(args)
    vim.cmd.source(args.file)
    vim.cmd.PackerCompile()
  end,
  desc = "Compile Packer plugins source.",
})

It's not working for me:

Error detected while processing BufWritePost Autocommands for "plugins.lua":
Error executing lua callback: /home/susensio/.config/nvim/lua/user/plugins.lua:22: attempt to
 index field 'cmd' (a function value)
stack traceback:
        /home/susensio/.config/nvim/lua/user/plugins.lua:22: in function </home/susensio/.con
fig/nvim/lua/user/plugins.lua:21>

@dave-kennedy
Copy link

dave-kennedy commented Dec 23, 2022

vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = "lua/plugins.lua",
  callback = function(args)
    vim.cmd.source(args.file)
    vim.cmd.PackerCompile()
  end,
  desc = "Compile Packer plugins source.",
})
  1. I think we want to use an autocommand group here, in case the file is sourced more than once.
  2. I don't think that pattern will ever match because it isn't absolute and doesn't contain a wildcard (see :help autocmd-pattern).
  3. Per the OP's original suggestion, we can omit the argument to vim.cmd.source().

So something like this:

vim.api.nvim_create_autocmd('BufWritePost', {
  group = vim.api.nvim_create_augroup('packer_compile', {}),
  pattern = '*/lua/plugins.lua',
  callback = 'source | PackerCompile'
})

I actually prefer to see a message when the command is executed. Also, */lua/plugins.lua might match a lot of files unintentionally, so I use the full path in the pattern:

vim.api.nvim_create_autocmd('BufWritePost', {
  group = vim.api.nvim_create_augroup('packer_compile', {}),
  pattern = vim.fn.expand('$HOME') .. '/dotfiles/vim/lua/plugins.lua',
  callback = function (ctx)
    vim.cmd.source()
    vim.cmd.PackerCompile()
    print('Packer compiled!')
  end
})

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

Successfully merging this pull request may close these issues.

6 participants