", function() local cmp = require("cmp") if cmp.visible() then cmp.confirm() else return '\\' end end, { desc = "Confir" /> ", function() local cmp = require("cmp") if cmp.visible() then cmp.confirm() else return '\\' end end, { desc = "Confir" /> ", function() local cmp = require("cmp") if cmp.visible() then cmp.confirm() else return '\\' end end, { desc = "Confir"/>

How to fix this nvim keymap? It's almost working but <Tab> returns extra backslash character when cmp isn't visible

83 Views Asked by At
vim.keymap.set("i", "<tab>", function()
    local cmp = require("cmp")
    if cmp.visible() then
        cmp.confirm()
    else
        return '\\<tab>'
    end
end, { desc = "Confirm", expr = true, noremap = true })

What to return or what command to run in else block to get default behavior?

2

There are 2 best solutions below

0
Pritesh Bhoi On

Try to use /t

vim.keymap.set("i", "<tab>", function()
    local cmp = require("cmp")
    if cmp.visible() then
        cmp.confirm()
    else
        return '\t'
    end   
    end, { desc = "Confirm", expr = true, noremap = true })

Please reply if any issue after changing this code.

Thanks

0
Michal Fedyna On

Ok, I found a solution

vim.keymap.set("i", "<tab>", function()
        return require("cmp").visible() and require("cmp").confirm() or "<tab>"
end, { desc = "Completion confirm", expr = true, noremap = true })