0

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.

NeoZoom.lua
  • 1,442
  • 14
  • 31
  • Hi @job_start, I believe a small example would help to understand what you want to achieve. – Vivian De Smedt Nov 23 '22 at 16:41
  • 1
    @VivianDeSmedt I just re-wrote it completely. – NeoZoom.lua Nov 23 '22 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. – Luc Hermitte Nov 23 '22 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. – NeoZoom.lua Nov 23 '22 at 17:12
  • If I understand correctly you are looking for abbreviations/snippets that use the name of the current file. While abbreviations do the work, snippets are much easier to maintain. There exist plenty dedicated plugins: https://vi.stackexchange.com/questions/7466/what-is-the-difference-between-the-vim-snippets-plugins – Luc Hermitte Nov 23 '22 at 17:16
  • @LucHermitte: I didn't mean that. I meant when I call :au it should show some autocmd belonging to git.lua, not plugins.lua. Putting expand('%:t') in different files still resulting in a single git.lua augroup, which is not what I want... – NeoZoom.lua Nov 23 '22 at 17:48
  • @LucHermitte I just made a temporary answer assuming what I really want to do cannot be achieved. Maybe it could make you understand my OP. – NeoZoom.lua Nov 23 '22 at 17:54
  • https://www.lua.org/pil/23.html – Matt Nov 23 '22 at 18:39
  • 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. – Luc Hermitte Nov 23 '22 at 19:21
  • Aren't you looking for: https://stackoverflow.com/a/21417614/15934 ? – Luc Hermitte Nov 23 '22 at 19:22
  • I second the usage of expand(<sfile>:t') that should give you the name of the script which you can then use later on. – Christian Brabandt Nov 24 '22 at 13:08

2 Answers2

1

Courtesy of the comments:

vim.fn.expand('<sfile>:p')
Tom Hale
  • 2,681
  • 14
  • 32
0

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

NeoZoom.lua
  • 1,442
  • 14
  • 31