Jump to definition in vertical/horizontal split

Hi,

one of the most basic things when navigating code is jumping around, and Neovim with LSP does it perfectly. With a large screen it’s convinient to jump to definitions in a vertical or horizontal split, so the context of the currently open file is not lost.

How can I do this with Neovim?

I tried to:

  • search documentation (":help lsp" and searching for “split”, “jump” and “definition”) - no luck
  • search github, found this closed issue, which has no answer except “This can be done with basic vimscript.”.
  • searched this forum - found only this topic, it discusses alternative file opening in vsplit for clangd

Basically I need to find/guess two commands similar to vim.lsp.buf.definition() that do the same but one is for vertical split and other for horizontal
I believe this should be come as a default commands.

Any ideas how to implement this?

3 Likes

Do you mean that you want to jump to definition in any available split or to open a new split and jump to definition there? In any case, I’m also interested in any of those.
The closest think I can think of is peek preview, but that is not “permanent”.
What I currently do is use go to references, which opens quick fix window and navigate from there

1 Like

I mean new split. Basically the goal is to temporarily go to another piece of code, without losing context in the current buffer (i.e. keep in sight) – and maybe navigate further into new files in a new splits. I have 49" screen, so it’s common to have 3-5 split screens. In my old setup that was out of the box (I think it was coc) and was super handy.

I also managed to somehow implement this with neovim, but I spend two evenings on that, and can’t find that config anymore :confused:

You can use tagfunc with this, here’s an example using it for a new tab.

thanks, but did you forget to post the link to an example?

Okay, so that’s the approach I found (still not sure if it’s the right one or if it’s efficient):

  map('n', '<leader>a', ':lua vim.lsp.buf.definition()<CR>')
  map('n', '<leader>v', ':vsplit | lua vim.lsp.buf.definition()<CR>')
  map('n', '<leader>s', ':belowright split | lua vim.lsp.buf.definition()<CR>')

(For horizontal split I tried to use “:new”, but it didn’t work somehow, so I stumbled upon “belowright split”).

2 Likes

Does it work? if it does, then I think it doesn’t matter how efficient it is?
You can have a look at the trouble plugin, they seem to have some features around easily navigating between definitions.
Also BQF provides preview on the quickfix list, so maybe that is enough to not loose context?

Yes, the pasted copy is what I have now in my config and seems to work fine (i.e. exactly what I wanted).

I use Trouble plugin for everything like lsp-references or diagnostics, yes.

Yep!

There’s a command for tagfunc for new split too, I just don’t remember it.

1 Like

Hey @divan will something like this be interesting for you?

Just found a plugin that allows to easily jump to definitions in nested editable/scrollable popups, very handy to keep the context.

3 Likes

Hi, I am interested in that nested editable/scrollable popups. Could you provide the link to it?

Sorry, forgot to link it:

1 Like

@danielo515 thank you. It’s slightly different, but need to try – might be even better. Thanks for the link!

@danielo515
Thanks for sharing! This is such a great plugin!

@divan
Sorry for interrupting your conversation.
Maybe a command from nvim-telescope/telescope.nvim may help.
Here is a sample of my config.

map('n', '<LEADER>jd', '<cmd>lua require"telescope.builtin".lsp_definitions()<CR>', {noremap=true, silent=true})
map('n', '<LEADER>jv', '<cmd>lua require"telescope.builtin".lsp_definitions({jump_type="vsplit"})<CR>', {noremap=true, silent=true})

I hope it helps!

1 Like

Glad you liked it! I use it a lot. You can even love the floating windows to become splits

wow, thanks @divan this works. genius