• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Dev Tips / vim.keymap.set() coc to confirm completion with lua

vim.keymap.set() coc to confirm completion with lua

Last updated on November 13, 2022 by Sal Ferrarello

I’m currently rewriting my Neovim configuration using lua rather than using vimscript. I’m using the coc.nvim plugin and want to map return (<CR>) to confirm completion.

The coc.nvim wiki has an entry to Use to confirm completion however their solution is in Vimscript.

inoremap <expr> <cr> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

As I’m still learning lua, it took me a little time to convert this Vimscript so I wanted to document my solution here.

vim.keymap.set(
    'i',
    '<CR>',
    function()
        if vim.fn['coc#pum#visible']() == 1 then
            -- coc Pop Up Menu (pum) is visible, confirm selection
            vim.fn['coc#pum#confirm']()
        else
            -- coco Pop Up Menu is not open, make no change to <CR>
            return "<CR>"
        end
    end,
    { expr = true }
);
Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Computing, Dev Tips, Programming, Solution Tagged With: lua, neovim, vim

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in