I am using coc.vim in neovim. coc.vim suggests the following mapping for jumping to the definition of a class/method.

nmap <silent> gd <Plug>(coc-definition)

If there is only a single matching definition, pressing gd will immediately open the definition in the current window.

If there are multiple matches, coc shows a preview window with the list of matching files. It is possible to scroll through the results with j/k. Pressing tab displays the following prompt, for opening the current selection.

Choose action:
[o]pen, (p)review, (q)uickfix, (t)abe, (d)rop, (v)split, (s)plit:
  • Is the above prompt a standard part of vim or is it part of coc.vim?
  • Is it possible to force this prompt, even if there is only a single hit?
  • If not, how can I force vim to open coc-definition in a new vertical split?
  • That doesn't look like any vim prompt I've ever seen – D. Ben Knoble Nov 4 '19 at 18:07
  • 1
    And @D.BenKnoble has seen a lot of things. – Maxim Kim Nov 4 '19 at 18:39

I inject my own way of opening files in CoC with:

let g:coc_user_config = {}
let g:coc_user_config['coc.preferences.jumpCommand'] = ':SplitIfNotOpen4COC'

Then the command is defined in my lh-vim-lib plugin to search if the buffer is already opened or not. Having more control is possible.

To use the prompt already provided by CoC, you may have to search in its source code.

  • 1
    Thank you. Setting coc.preferences.jumpCommand to vsp worked. I have just started learning vim, so I couldn't fully understand lh#coc#_split_open. I don't want to copy paste blindly, and will give it a second look after learning vimscript. – Joyce Babu Nov 4 '19 at 20:23
  • lh#coc#_split_open() is a COC specific wrapper around another function of mine: the one that is a kind of :sb_or_sp (if it were to exist). – Luc Hermitte Nov 4 '19 at 23:52
  • After reading a few tutorials, I could understand parts of your function code. The first argument, if prefixed with +, is stored in where variable and is executed as an ex command when the file is not open in any existing window. But what is the content of the where variable? Is it set by coc, or do you use pass it when manually invoking SplitIfNotOpen4C0C? – Joyce Babu Nov 5 '19 at 6:48
  • It's COC doing. It encodes a few things in the filenames in order to specify the position the cursor shall jump to. NB: my function will return a negative number only if it cannot jump into a window (new or not) containing the requested file. – Luc Hermitte Nov 5 '19 at 15:19
  • 1
    @LucHermitte I resolved this issue quite satisfactorily based on what I gleaned from github.com/neoclide/coc.nvim/issues/1249, I have 4 different vim maps which implement the 4 ways to open definition: regular (in present window), in split, in vsplit, in new tab. The main thing was that the coc instructions/doc never mentioned these alternate ways of going to definition. As for going to references or implementation I use them less and also the cocList comes with binds for these different opening modalities. – Steven Lu Nov 24 '19 at 19:58

You can call the action jumpDefinition with a command as argument:

nmap <silent> gs :call CocAction('jumpDefinition', 'split')<CR>
nmap <silent> gd :call CocAction('jumpDefinition', 'vsplit')<CR>
nmap <silent> gt :call CocAction('jumpDefinition', 'tabe')<CR>

To get more information:

:h coc-action-jumpDefinition
  • 1
    Welcome to Vi and Vim! We prefer to include the salient points from a link in the body of the post (via edit) to avoid issues with linkrot. – D. Ben Knoble Feb 28 at 17:34
  • 1
    Ok, fixed. Thanks for the advice – Mateus C Feb 28 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.