c++neovimfile-extensionfile-type

How to make Neovim associate ".tpp" file extension with C++ files?


I would like Neovim to associate files with .tpp extension with C++.

How can I achieve this?

P.S.:

I would like nvim-lspconfig and nvim-cmp Neovim plugins to also associate this file extension with C++.

I want to indicate that Neovim is writing me Unable to handle compilation, expected exactly one compiler job in '' clang (fe_expected_compiler_job) [1, 1] after opening .tpp file. What could it mean? Proofing screenshot of an error message


Solution

  • You could use:

    1. :h vim.filetype.add():

    vim.filetype.add({
      extension = {
        tpp = "cpp",
      },
    })
    

    2. :h ftdetect

    In nvim/after/ftdetect add a module with an autocmd to set the filetype accordly:

    -- nvim/after/ftdetect/cpp.lua
    vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
      group = vim.api.nvim_create_augroup("CustomCppDetection", {}),
      desc = "Set .ttp files to C++",
      callback = function(ev)
        if vim.fn.expand("%"):sub(-4) == ".tpp" then
          vim.api.nvim_set_option_value("filetype", "cpp", { buf = ev.buf })
        end
      end,
    })
    

    Full documentation on :h filetype