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?
Ctags is a tool that will sift through your code, indexing methods, classes, variables, and other identifiers, storing the index in a tags file.
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.
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:
<c-]>
: Jumps to the tag definition of the word under cursor updating tag stack.<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.<c-w>z
: Close preview window.<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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With