Haiyuan Zhang
Haiyuan Zhang

Reputation: 42792

how to map keys for popup menu in vim

After a completion try, omnicppcomplete will display all the possible items in the pop up menu . To select an certain item in the menu, one should use <C-N> and <C-p> to switch back and forth between different items. I feel that it is very inconvient . It should be very cool if j and k can be used to to take place of <C-N> and <C-P> . so how should I do ?

Upvotes: 7

Views: 4234

Answers (3)

run_the_race
run_the_race

Reputation: 2318

CTRL+J and CTRL+K instead: (so you can type j and k)

inoremap <expr><C-J> pumvisible() ? "\<C-n>" : "\<C-J>"
inoremap <expr><C-K> pumvisible() ? "\<C-p>" : "\<C-K>"

Bonus: <ENTER> to select the option

inoremap <expr><Cr>  pumvisible() ? "\<C-y>" : "\<Cr>"

Upvotes: 2

skeept
skeept

Reputation: 12413

I prefer using the tab key for completion (I am not sure where I got this from):

"tab complete
function! InsertTabWrapper(direction)
    let col = col('.') - 1
    if !col || getline('.')[col - 1] !~ '\k'
        return "\<tab>"
    elseif "backward" == a:direction
        return "\<c-p>"
    else
        return "\<c-n>"
    endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper ("forward")<cr>
inoremap <s-tab> <c-r>=InsertTabWrapper ("backward")<cr>

Upvotes: 2

user256497
user256497

Reputation:

function! OmniPopup(action)
    if pumvisible()
        if a:action == 'j'
            return "\<C-N>"
        elseif a:action == 'k'
            return "\<C-P>"
        endif
    endif
    return a:action
endfunction

inoremap <silent>j <C-R>=OmniPopup('j')<CR>
inoremap <silent>k <C-R>=OmniPopup('k')<CR>

Upvotes: 10

Related Questions