21

How to get the current file type in lua with neovim? I tried following but all of them returns nil.

print(vim.g.filetype) print(vim.b.filetype) print(vim.w.filetype) print(vim.t.filetype) print(vim.v.filetype) 

1 Answer 1

36

filetype is a buffer-local option, so you can access it with:

vim.bo.filetype 

From :h lua-vim-options:

From Lua you can work with editor |options| by reading and setting items in these Lua tables: vim.w ... vim.bo *vim.bo* Get or set buffer-scoped |local-options|. Invalid key is an error. Example: vim.bo.buflisted = true print(vim.bo.comments) vim.wo ... 
2
  • 1
    in my init.lua printing this doesn't show anything, but :lua print(vim.bo.filetype) shows the correct type - why is that? Commented Jun 6, 2024 at 19:09
  • Perhaps because your print statement occurs before the editor has detected the filetype? Commented Jun 10, 2024 at 19:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.