相关文章推荐
高兴的烈酒  ·  python 按钮形状 ...·  1 年前    · 
飞翔的投影仪  ·  Android之android.os.Dea ...·  1 年前    · 
神勇威武的匕首  ·  Matlab ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

And use it for key mapping like so:

map("", "<Leader>f", ":CocCommand prettier.forceFormatDocument<CR>") 
map("", "<Leader>f", ":RustFmt<CR>")

I want to use :RustFmt only for .rs files and :CocCommand prettier.forceFormatDocument for all the other files.

Is this possible to do with vim.api.nvim_set_keymap and if so how could I do it?

@DoktorOSwaldo , not quite. The docs on nvim_set_keymap says it's option can not accept <buffer>. neovim.io/doc/user/api.html#nvim_set_keymap() – Slava.In Jul 14, 2022 at 19:40 @Slava.In, Sorry I removed the flag, but the comment stays. I saw that it is not the same. But the question has be answered multiple times. There are two ways. You can use a filetype specific autocmd or, in my opinion better, use the ftplugin directory as unrealapex said. – Doktor OSwaldo Jul 15, 2022 at 5:08 I would like to know how to add the filename local filename = vim.api.nvim_buf_get_name(0):gsub('^.*/', '') at the beginning of the description: ` { desc = "string" }` so I that I can debug my mappings, differentiating them from other mappings. – SergioAraujo Jul 27, 2022 at 20:37

Thanks to @DoktorOSwaldo and @UnrealApex I was able to resolve the issue using ftplugin.

Steps:

  • Create ftplugin directory inside ~/.config/nvim.
  • Inside ftplugin directory create a file rust.lua.
  • Inside rust.lua import map util and define key mapping.
  • local map = require("utils").map
    -- Format document
    map("", "<Leader>f", ":RustFmt<CR>")
    

    For languages other than Rust use the following command to get the full list of possible file names (.vim can be switched to .lua):

    :exe 'Lexplore ' . expand('$VIMRUNTIME') . '/syntax'
    

    You can create a format function in your utils.lua configuration file :

    function M.format()
      if vim.bo.filetype == 'rust' then
        vim.cmd('RustFmt')
        vim.cmd('CocCommand prettier.forceFormatDocument')
    

    and define your key mapping like this :

    map("", "<Leader>f", "<cmd>:lua require('utils').format<CR>")
                    See the complete code un the first post, code return M as table (standard structure for a Neovim plugin in Lua).
    – lcheylus
                    Apr 12 at 15:56
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.