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?
–
–
–
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>")
–
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.