Magika-powered filetype detection for Neovim.
Install the magika CLI first. It needs to be available in $PATH:
magika --version{
"moltinginstar/magika.nvim",
lazy = false,
opts = {},
}-
Clone the repository into your Neovim package directory, for example:
git clone https://github.com/moltinginstar/magika.nvim ~/.local/share/nvim/site/pack/moltinginstar/start/magika.nvim -
Add the following line to your
init.lua:require("magika").setup()
Neovim already has good filetype detection. magika.nvim is for the cases where that detection falls back to nothing useful.
By default, Magika only runs when the current buffer filetype is:
- empty
text
If Neovim has already detected something more specific, magika.nvim leaves it alone.
The plugin only runs for normal file-backed buffers. It skips scratch buffers, terminals, help buffers, quickfix buffers, and anything else with a non-empty buftype. It does not read the buffer into Lua, does not create temp files, and does not pipe buffer contents to stdin. It simply passes the current file path to the Magika CLI.
This means:
- unsaved changes are not used for detection
- unnamed buffers are skipped
- files that do not exist on disk yet are skipped
Magika is called asynchronously with vim.system(), and results are cached in memory using the file path, mtime, and size.
require("magika").setup({
enabled = true,
filetypes = { "", "text" },
magika_cmd = { "magika", "--json" },
timeout_ms = 1500,
confidence_threshold = 0.8,
cache = true,
})filetypes controls when Magika is allowed to run, based on the buffer's current filetype.
Magika only applies its result if the buffer still has an allowed filetype when the async classification finishes.
For example, this is the default:
require("magika").setup({
filetypes = { "", "text" },
})This lets Magika run on every normal file-backed buffer:
require("magika").setup({
filetypes = "*",
})This lets you decide yourself:
require("magika").setup({
filetypes = function(ft)
return ft == "" or ft == "text" or ft == "conf"
end,
}):MagikaDetect: run Magika on the current buffer
The command forces a Magika run for the current buffer. It still operates on the file on disk, not the unsaved buffer contents.
Create a small Excalidraw file:
cat > /tmp/magika-test.excalidraw <<'EOF'
{
"type": "excalidraw",
"version": 2,
"source": "magika.nvim test",
"elements": [],
"appState": {},
"files": {}
}
EOFThen open it:
nvim /tmp/magika-test.excalidrawNeovim does not currently detect .excalidraw by default, but Magika detects the file as JSON, so :set filetype? should show:
filetype=jsonThis project is licensed under the MIT License.