As title. Say I have a line to create an augroup located in file git.lua. What I want to achieve is that that line will always create an augroup named git.lua without hard-coding it by the string "git.lua", so I can renamed the file and no code needs to change. I did try calling nvim API like vim.fn.bufname(and removing the prefix to get git.lua) in git.lua, but this resulted in an augroup of name plugins.lua, since this is where git.lua got imported/required. So what's the correct way to achieve this?

In short: I want to have a line of code to create an augroup, such that I can renamed that file and that line will always create an augroup as per the new name.

  • Hi @job_start, I believe a small example would help to understand what you want to achieve. Nov 23, 2022 at 16:41
  • 1
    @VivianDeSmedt I just re-wrote it completely. Nov 23, 2022 at 17:06
  • What snippet engine are you using? All of them should be able to expand the result of expand('%'). See :h expand() for the filename modifiers. Nov 23, 2022 at 17:06
  • @LucHermitte Not sure about the first question. I did try expand('%:t') but even if I put it in git.lua it created a augroup called plugins.lua, which is the file that imports git.lua. I want to create an augroup called git.lua. Nov 23, 2022 at 17:12
  • 1
    Sorry I've completely misundertstood what you were trying to accomplish. Beside I exclusively program vim in vimscript language. In pure old fashioned and portable (?) way, we can obtain the name of the current file with let s:sname = expand('<sfile>:t'). It has to be done at global file level. Not within a function! I leave plugin developers for nvim give you a nvim answer. Nov 23, 2022 at 19:21

2 Answers 2


Courtesy of the comments:


It seems that hard-coding the filename for each file is the simplest way to achieve what you want to do. So the code is:

vim.api.nvim_create_augroup('LSP.lua', { clear = true })

vim.api.nvim_create_autocmd('CursorHold', {
  group = 'LSP.lua',
  pattern = '*',

That is: you cannot replace xxx.lua with vim.fn.expand('%:t').

Here is an example of some state-of-the-art nvim-v0.9 plugin using the same method: https://github.com/akinsho/bufferline.nvim/blob/e70be6232f632d16d2412b1faf85554285036278/lua/bufferline.lua#L34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.