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.

You are viewing a single thread.
View all comments
1 point

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.

permalink
report
reply
2 points

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.

permalink
report
parent
reply
3 points

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.

permalink
report
parent
reply
2 points
*

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()?

permalink
report
parent
reply

Neovim

!neovim@sopuli.xyz

Create post

Neovim is a modal text editor forked off of Vim in 2014. Being modal means that you do not simply type text on screen, but the behavior and functionality of the editor changes entirely depending on the mode.

The most common and most used mode, the “normal mode” for Neovim is to essentially turn your keyboard in to hotkeys with which you can navigate and manipulate text. Several modes exist, but two other most common ones are “insert mode” where you type in text directly as if it was a traditional text editor, and “visual mode” where you select text.

Neovim seeks to enable further community participation in its development and to make drastic changes without turning it in to something that is “not Vim”. Neovim also seeks to enable embedding the editor within GUI applications.

The Neovim logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

Community stats

  • 40

    Monthly active users

  • 72

    Posts

  • 178

    Comments