ext)
if ext == 'md' then
return 'markdown'
elseif ext == 'rst' then
return 'rst'
end
end,
},
})
<
To add a fallback match on contents, use >lua
vim.filetype.add {
pattern = {
['.*'] = {
function(path, bufnr)
local content = vim.api.nvim_buf_get_lines(bufnr, 0, 1, false)[1] or ''
if vim.regex([[^#!.*\\<mine\\>]]):match_str(content) ~= nil then
return 'mine'
elseif vim.regex([[\\<drawing\\>]]):match_str(content) ~= nil then
return 'drawing'
end
end,
{ priority = -math.huge },
},
},
}
<
Parameters: ~
• {filetypes} (`table`) A table containing new filetype maps (see
example).
• {pattern}? (`vim.filetype.mapping`)
• {extension}? (`vim.filetype.mapping`)
• {filename}? (`vim.filetype.mapping`)
*vim.filetype.get_option()*
vim.filetype.get_option({filetype}, {option})
Get the default option value for a {filetype}.
The returned value is what would be set in a new buffer after 'filetype'
is set, meaning it should respect all FileType autocmds and ftplugin
files.
Example: >lua
vim.filetype.get_option('vim', 'commentstring')
<
Note: this uses |nvim_get_option_value()| but caches the result. This
means |ftplugin| and |FileType| autocommands are only triggered once and
may not reflect later changes.
Attributes: ~
Since: 0.9.0
Parameters: ~
• {filetype} (`string`) Filetype
• {option} (`string`) Option name
Return: ~
(`string|boolean|integer`) Option value
vim.filetype.match({args}) *vim.filetype.match()*
Perform filetype detection.
The filetype can be detected using one of three methods:
1. Using an existing buffer
2. Using only a file name
3. Using only file contents
Of these, option 1 provides the most accurate result as it uses both the
buffer's filename and (optionally) the buffer contents. Options 2 and 3
can be used without an existing buffer, but may not always provide a match
in cases where the filename (or contents) cannot unambiguously determine
the filetype.
Each of the three options is specified using a key to the single argument
of this function. Example: >lua
-- Using a buffer number
vim.filetype.match({ buf = 42 })
-- Override the filename of the given buffer
vim.filetype.match({ buf = 42, filename = 'foo.c' })
-- Using a filename without a buffer
vim.filetype.match({ filename = 'main.lua' })
-- Using file contents
vim.filetype.match({ contents = {'#!/usr/bin/env bash'} })
<
Parameters: ~
• {args} (`table`) Table specifying which matching strategy to use.
Accepted keys are:
• {buf}? (`integer`) Buffer number to use for matching.
Mutually exclusive with {contents}
• {filename}? (`string`) Filename to use for matching. When
{buf} is given, defaults to the filename of the given buffer
number. The file need not actually exist in the filesystem.
When used without {buf} only the name of the file is used
for filetype matching. This may result in failure to detect
the filetype in cases where the filename alone is not enough
to disambiguate the filetype.
• {contents}? (`string[]`) An array of lines representing file
contents to use for matching. Can be used with {filename}.
Mutually exclusive with {buf}.
Return (multiple):