Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

tags

ctags

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?

like image 635
user1004985 Avatar asked Nov 09 '15 00:11

user1004985


People also ask

Where are ctags stored?

Ctags is a tool that will sift through your code, indexing methods, classes, variables, and other identifiers, storing the index in a tags file.

What is ctags Linux?

The ctags command creates a tags file for use with the ex and vi editors from the specified C, Pascal, FORTRAN, yacc, lex, and LISP source files. The tags file consists of locators of programming language specific objects (such as functions and type definitions) within the source files.


1 Answers

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.

like image 137
Vitor Avatar answered Oct 19 '22 08:10

Vitor