I’m using NvChad and in ~/.config/nvim/lua/core/mappings.lua there is a keybinding for LSP code action:

  ["<leader>ca"] = {
    function()
      vim.lsp.buf.code_action()
      -- TODO: write buffer to file
    end,
    "LSP code action",
},

this keybinding applies the code action, but does not write to file. I want to write changes to file as soon as I’ve applied the code action.

How can I use the documentation at https://neovim.io/doc/ to find the correct function? I’ve tried looking for a write() function but I could not find anything I can call from lua.

  • ash
    link
    fedilink
    English
    11 year ago

    You can call commands and vim functions from lua. To write the current buffer you can: vim.cmd.write(). Vim functions can be called with vim.fn.

    It is completely fine to use vim functiona and not just pure lua, afaik some vim functions are not ever gonna get a lua alternative cause there is no need.

    • Agility0971OP
      link
      English
      21 year ago

      Thanks for the function signature, but how do I find the documentation for it? Searching for vim.cmd.write() on the website does not return anything. Maybe it takes in optional arguments or returns some sort of error.

      • ash
        link
        fedilink
        English
        31 year ago

        vim.cmd.<command> calls a command. So vim.cmd.write is effectively the same as :write, the arguments passed to the function are the same that the command would take. Check :h vim.cmd() and for a specific command, e.g. write, you can check :h :write.

        • Agility0971OP
          link
          English
          2
          edit-2
          1 year ago

          Ok, I found it. list of all vim functions are here :h builtin-function-list and all commands are here: :h ex-cmd-index.

          From using other programming languages, I’m always looking for a list of functions with a complete function signature, “gotcha” notes, when errors can occur and so on. But vim.cmd() does not mention anything about if the command that is being passed in will succeed or fail. Nor does it mention anything about a return value. :h :write mentions that a write might fail, but how could that be detected if it’s being called as vim.cmd.write()?

          • ash
            link
            fedilink
            English
            11 year ago

            You can wrap the call in pcall, which is a lua builtin for catching errors, which would suppress the error and let you know if the command failed.

            You could for example do:

            local ok, res = pcall(vim.cmd.write)
            if not ok
            then
                vim.notify('write failed with: ' .. res)
            end
            

            There are both lua and vim functions for writing to files but I recommend to not use them in this scenario, they write to the file directly and dont trigger autocommands.

            I understand your frustration with no consitent error reporting and clear api, but I guess that’s the consequence of the entire history of vi and vim and trying to be backwards compatible.