Home Explore Blog CI



neovim

36th chunk of `runtime/doc/lua.txt`
14fa0c9f65170bf46cac6ca83cde46f7d1ae2acafef5c4080000000100000fa4
 respectively. Use a |FileType| autocommand to configure
any local options for these windows and their respective buffers.

Rather than a |hit-enter-prompt|, messages shown in the cmdline area that do
not fit are appended with a `[+x]` "spill" indicator, where `x` indicates the
spilled lines. To see the full message, the |g<| command can be used.



==============================================================================
Lua module: vim.filetype                                        *vim.filetype*

vim.filetype.add({filetypes})                             *vim.filetype.add()*
    Add new filetype mappings.

    Filetype mappings can be added either by extension or by filename (either
    the "tail" or the full file path). The full file path is checked first,
    followed by the file name. If a match is not found using the filename,
    then the filename is matched against the list of |lua-pattern|s (sorted by
    priority) until a match is found. Lastly, if pattern matching does not
    find a filetype, then the file extension is used.

    The filetype can be either a string (in which case it is used as the
    filetype directly) or a function. If a function, it takes the full path
    and buffer number of the file as arguments (along with captures from the
    matched pattern, if any) and should return a string that will be used as
    the buffer's filetype. Optionally, the function can return a second
    function value which, when called, modifies the state of the buffer. This
    can be used to, for example, set filetype-specific buffer variables. This
    function will be called by Nvim before setting the buffer's filetype.

    Filename patterns can specify an optional priority to resolve cases when a
    file path matches multiple patterns. Higher priorities are matched first.
    When omitted, the priority defaults to 0. A pattern can contain
    environment variables of the form "${SOME_VAR}" that will be automatically
    expanded. If the environment variable is not set, the pattern won't be
    matched.

    See $VIMRUNTIME/lua/vim/filetype.lua for more examples.

    Example: >lua
        vim.filetype.add({
          extension = {
            foo = 'fooscript',
            bar = function(path, bufnr)
              if some_condition() then
                return 'barscript', function(bufnr)
                  -- Set a buffer variable
                  vim.b[bufnr].barscript_version = 2
                end
              end
              return 'bar'
            end,
          },
          filename = {
            ['.foorc'] = 'toml',
            ['/etc/foo/config'] = 'toml',
          },
          pattern = {
            ['.*/etc/foo/.*'] = 'fooscript',
            -- Using an optional priority
            ['.*/etc/foo/.*%.conf'] = { 'dosini', { priority = 10 } },
            -- A pattern containing an environment variable
            ['${XDG_CONFIG_HOME}/foo/git'] = 'git',
            ['.*README.(%a+)'] = function(path, bufnr, 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`)
                  

Title: Lua API: vim.filetype.add - Adding New Filetype Mappings
Summary
This section describes the `vim.filetype.add()` function, which allows adding new filetype mappings based on file extension, filename, or Lua pattern matching. It explains how to specify the filetype as a string or a function that takes the file path and buffer number as arguments. The function can also return a second function to modify the buffer's state. Filename patterns can include priorities and environment variables. The section provides examples of adding filetype mappings using extensions, filenames, and Lua patterns, including fallback matching based on file contents.