How to cause ctags to show tag definition on vertical split without opening a new split?
Asked Answered
I

2

10

I am using ctags and I added map <C-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR> to my vimrc. However, this opens a new vertical split everytime. Is there a way to show tag definitions on vertical split without opening a new one everytime?

Update: I would also like to know if there is a way to use the ctag stack normally with this. That is, use ctrl + t to pop a location from the stack?

Isbell answered 9/11, 2015 at 0:13 Comment(3)
as of VIM 8.0 (at least) you can use CTRL+W+] to go for a tag in horizontal splitLicit
@VolodymyrBoiko and how does one get a vertical split?Pasargadae
@Yogesch probably there is some configs to get it vertical by default but I always just move it manually to right like this: CTRL+w+] CTRL+w+L (capital L)Licit
F
12

The following command achieves the result you're looking for:

:execute "vertical ptag " . expand("<cword>")

So, this mapping should also work:

nnoremap <C-]> :execute "vertical ptag " . expand("<cword>")<CR>

You might want to set 'previewheight' to a higher value.

Update

As an alternative solution and if you want to keep navigating in tags, then the following can be used:

function! FollowTag()
  if !exists("w:tagbrowse")
    vsplit
    let w:tagbrowse=1
  endif
  execute "tag " . expand("<cword>")
endfunction

nnoremap <c-]> :call FollowTag()<CR>

Nevertheless, I think you should consider revising the need to create such a shortcut by taking the following standard Vim shortcuts into account:

  1. <c-]> : Jumps to the tag definition of the word under cursor updating tag stack.
  2. <c-w>} : Opens a preview window with the location of the tag definition. The cursor does not change its position, so tag stack is not updated.
  3. <c-w>z : Close preview window.
  4. <c-w>v : Split current window in two, keeping the cursor position.

So, you can use <c-w>}if you want to quickly check the tag declaration, followed by <c-w>z to close it. But if you want to navigate, then you can simply use <c-w>v to create a split followed by the standard <c-] to navigate in the tags. When you're done with it, you can simply close the window with <c-w>c.

Fluecure answered 10/11, 2015 at 12:4 Comment(7)
Thank you, this works fine if I do set previewheight=100. However, the tag stack would always be empty. Could you explain what vertical ptag does? Is this the reason for the tag stack to be empty?Isbell
Why would you need to update the stack if you did not leave the current buffer? Could you update your question explaining what exactly you are trying to achieve?Fluecure
Updated the question. What I meant was, I was trying to do a ctrl+T to go to the previous location (pop a location off the stack)Isbell
Let's assume you have one window opened and press the shortcut we're trying to create. According to your requirements, a new vertical window is opened and the cursor moves to the declaration of the keyword we were in the original window. If we pop from the tag stack, both windows will be at the same location! What is the benefit of that? WHy not simply close the new window?Fluecure
Suppose func1 calls func2 which calls func3. I lookup func1 which opens in a new window. Then I lookup func2 inside it and then func3. I was also looking for a way to go back the way I came.Isbell
Thanks again for the detailed explanation. The command however says "E580: :endif without :if:", but the solution without using any special mappings works too.Isbell
I should have tested it. I've updated the solution. It's no longer an one liner, but it works.Fluecure
U
0

The command below should do what you want, at least in a hack-ish way:

nnoremap <C-]> :only<bar>vsplit<CR>:execute "tag" . expand('<cword>')<CR>
Unreserve answered 9/11, 2015 at 6:30 Comment(1)
Thank you. However, it doesn't go to the tag definition properly after the first timeIsbell

© 2022 - 2024 — McMap. All rights reserved.