3

我试图让 vim 允许我用 tab 键在自动完成弹出列表中循环。它适用于 tab 但不适用于 s-tab (shift-tab)。似乎 shift-tab 在应用 CP 之前以某种方式取消了自动完成菜单

有人有什么想法吗?

function InsertTabWrapper(direction)
  if pumvisible()
    if "forward" == a:direction
      return "\<C-N>"
    else
      return "\<C-P>"
    endif
  endif
  let col = col('.') - 1
  if !col || getline('.')[col - 1] !~ '\k' 
    return "\<tab>"
  else
    return "\<c-x>\<c-o>"
  endif
endfunction

inoremap <tab> <c-r>=InsertTabWrapper("forward")<cr>
inoremap <s-tab> <c-r>InsertTabWrapper("backward")<cr>
4

1 回答 1

8

<c-r>您在<s-tab>映射之后错过了等号“=” 。

但是,我建议这样做:

function! InsertTabWrapper()
  if pumvisible()
    return "\<c-n>"
  endif
  let col = col('.') - 1
  if !col || getline('.')[col - 1] !~ '\k'
    return "\<tab>"
  else
    return "\<c-x>\<c-o>"
  endif
endfunction
inoremap <expr><tab> InsertTabWrapper()
inoremap <expr><s-tab> pumvisible()?"\<c-p>":"\<c-d>"
  1. 使用<expr>映射。更好看,更清晰(很多人不知道的<c-r>=事情。
  2. 像这样映射<s-tab>,您可以在插入模式下取消缩进。
于 2012-03-18T02:59:50.797 回答