0

I'm using the CoC plugin for which works great.

This command remaps the enter key so we can use enter to confirm selection of the suggestion popover.

This mapping works great, except that it stays in insert mode after accepting the selection:

inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"`

From https://github.com/neoclide/coc.nvim/wiki/Completion-with-sources#use-cr-to-confirm-completion

How can I modify this to return back to normal mode after I hit enter?

Any ideas? Thanks!

Jamis Charles
  • 5,827
  • 8
  • 32
  • 42
  • Do you actually understand what that mapping (not "remapping") does? Do you know how to leave insert mode? – romainl May 12 '22 at 05:40
  • @romainl I do know how to manually leave insert mode. I want the acceptance of the suggestion to apply the suggested code, then be in normal mode so I can navigate on easily. I'm not entirely certain what this mapping does. – Jamis Charles May 12 '22 at 06:14

1 Answers1

1

First, let's deconstruct the mapping you got from that page:

inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
  • inoremap makes it a non-recursive insert mode mapping. i means "insert mode" and nore means "non-recursive". I know the remap at the end makes it tempting to call those things "remaps" or "remappings" but they are not. They are just "mappings".

  • <expr> makes it an expression mapping. It means that the right-hand side is an expression that is going to be evaluated at runtime to produce the actual RHS.

  • pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>" is what Vim calls a "trinary operator" (AKA ternary operator): :help trinary. In plain english, it reads like this: "if the popup menu is visible, return <C-y>, if not, return <C-g>u<CR>".

    :help complete_ctrl-y accepts the current suggestion in the popup menu.

    :help ctrl-g_u prevents Vim from inserting an undo point before <CR>. It is not strictly necessary but good practice.

You want to adjust the mapping's behaviour when you accept a suggestion so it is the "\<C-y>" part that must be changed.

Now…

  • you already know what key to press to leave insert mode,
  • you can take a look at :help key-notation if you are unsure how to represent that key,
  • you know what part of the mapping you need to change,
  • you should get an idea of how to proceed just by looking at the two last parts of the "trinary" operator.
romainl
  • 186,200
  • 21
  • 280
  • 313
  • Thank you this is very helpful context, and provided some great reading and learning. Here are things that I'm trying but aren't working: `inoremap pumvisible() ? "\\" : "\u\"` - Seems to ignore the accept and just switches to normal mode without inserting the accepted selection. I tried mapping it to a function but ran into the exact same issues: `" inoremap pumvisible() ? acceptSelectionAndExitInsertMode() : "\u\"` Any ideas? – Jamis Charles May 12 '22 at 16:48
  • Use `` instead of ``. – romainl May 12 '22 at 18:11
  • `inoremap pumvisible() ? "\\" : "\u\"` Leads to exactly the same issue. I'm not sure why these expressions seem to focus on the last part only (similar to JS maybe?). This command will ignore the selection and insert 'hello'`inoremap pumvisible() ? "\Hello" : "\u\"` – Jamis Charles May 12 '22 at 19:10
  • 1
    The `` variant works the way you want in [`$ vim -Nu NONE`](https://i.imgur.com/TJEqU9o.gif). – romainl May 13 '22 at 05:09