一千萬個為什麽

搜索

自動完成彈出菜單:如果未選擇任何項目,則輸入觸發換行符

I like using the key to confirm my selection in the popup menu. However I don't like the key doing absolutely nothing when no item was selected in the menu -- in this case, it usually means I don't want to confirm a completion, I just want to insert a newline.

我當前重新映射

inoremap   pumvisible() ? "\" : "\"

將“確認選擇”並關閉彈出菜單,即使沒有選擇任何項目 - 即光標停留在同一位置。我可以在上面的三元操作或其他方法中使用另一個函數,它會給我所需的行為嗎?

最佳答案

我用一個相當hacky解決方案解決了這個問題。我只是重新映射我想用來滾動彈出菜單的鍵,以便在滾動菜單時遞增/遞減計數器。如果計數器為零,我們將在彈出菜單中插入換行符而不是“確認”我們的選擇。請註意,這些是使用 neocomplete插件開發的,其中包含選項 let g:neocomplete #enable_auto_select = 0 </代碼>。

I personally just use / and / but included some other remaps for people who prefer different keys.

"Reset the counter with autocmds where possible
au BufEnter * let b:tabcount=0
au InsertEnter * let b:tabcount=0
"Helper functions
function! s:tabincrease() "use this inside  remaps
  let b:tabcount+=1
  return "" "returns empty string so can be used inside 
endfunction
function! s:tabdecrease() "use this inside  remaps
  let b:tabcount-=1
  return ""
endfunction
function! s:tabreset() "use this inside  remaps
  let b:tabcount=0
  return ""
endfunction
"Map the default keys for navigating completion popup menu
inoremap   pumvisible() ? "\".tabreset() : "\"
inoremap   pumvisible() ? "\".tabreset() : "\"
inoremap   tabincrease()."\"
inoremap   tabdecrease()."\"
"Map custom keys for confirming/cancelling a menu selection
inoremap   pumvisible() ? b:tabcount==0 ? "\\" : "\".tabreset() : "\"
inoremap   pumvisible() ? "\".tabreset() : "\"
inoremap   pumvisible() ? "\\".tabreset() : "\"
"Map custom keys for scrolling through the menu
inoremap   pumvisible() ? tabincrease()."\" : "\"
inoremap   pumvisible() ? tabdecrease()."\" : "\"
inoremap   pumvisible() ? tabincrease()."\" : "\"
inoremap   pumvisible() ? tabdecrease()."\" : "\"
inoremap   pumvisible() ? tabincrease()."\" : "\"
inoremap   pumvisible() ? tabdecrease()."\" : "\"

轉載註明原文: 自動完成彈出菜單:如果未選擇任何項目,則輸入觸發換行符