10

I want to select the 6th item (here readImage) in the auto-completion items.

enter image description here

The normal way i knew is pressing ctrln for 6 times until the matched word is selected and then press enter.

I tried different solutions:

  1. With the mouse:

    :set mouse=a 
    

    then moving my cursor on the 6th word,and clicking mouse,no effect.

  2. By entering a number:

    Pressing 6, no effect.

What is your smart way to select matched item in auto-completion?

My vim version on mouse is the following:

vim --version | tr -s  " " "\n" |grep mouse
+mouse_sgr
-mouse_sysmouse
+mouse_urxvt
+mouse_xterm
+mouse
-mouseshape
+mouse_dec
-mouse_gpm
-mouse_jsbterm
+mouse_netterm
showkey
  • 1,130
  • 2
  • 11
  • 30

2 Answers2

4

You can select the currently highlighted :h ins-completion by pressing Ctrl-y and discard the completions and go back to original text by pressing Ctrl-E. For more information, see :h complete_CTRL-E and :h complete_CTRL-Y

  • Ctrl-E
    When completion is active you can use CTRL-E to stop it and go back to the originally typed text. The CTRL-E will not be inserted.

  • Ctrl-Y
    When the popup menu is displayed you can use CTRL-Y to stop completion and accept the currently selected entry. The CTRL-Y is not inserted. Typing a space, Enter, or some other unprintable character will leave completion mode and insert that typed character.

For some more completion-menu keys, you can read :h popupmenu-keys

3N4N
  • 5,684
  • 18
  • 45
  • Looks like OP wants to be able to select non-current item with a mouse or some shortcut avoiding C-n/C-p – Maxim Kim May 15 '19 at 07:34
1

patch 8.1.1068 introduced a new function :h complete_info:

    patch 8.1.1068: cannot get all the information about current completion

    Problem:    Cannot get all the information about current completion.
    Solution:   Add complete_info(). (Shougo, Hirohito Higashi, closes #4106)

You can create map like this:

" map 1-9 to nth item
" map shift+{1-9} to last nth item
let s:shiftKeys = '!@#$%^&*('
for s:i in range(1, 9)
  exe printf('inoremap <expr> %d pumvisible() ? <sid>select_pum(%d - 1) : %d ', s:i, s:i, s:i)
  let skey = s:shiftKeys[s:i-1]
  exe printf('inoremap <expr> %s pumvisible() ? <sid>select_pum(-%d) : "%s" ', skey, s:i, skey)
endfor

" map 0 to original item
inoremap <expr> 0 pumvisible() ? '<c-e>' : 0

function! s:select_pum(index)
  let compInfo = complete_info()
  let idx = a:index >= 0 ? a:index : a:index + len(compInfo.items)
  let d = idx - compInfo.selected
  return repeat( d > 0 ? "\<c-n>" : "\<c-p>", abs(d))
endfunction
dedowsdi
  • 6,248
  • 1
  • 18
  • 31