vim: Open tag in new tab

VimVim Plugin

Vim Problem Overview


Is there a plugin or script to open ctags entries in a new tab? I'd like to put my cursor over a function, press ctrl+] and have the entry open in another tab. I'd also like if I visually select an entry, for ctrl+] to still work and open in a new vim tab.

Vim Solutions


Solution 1 - Vim

You can

C-wC-]C-wT

To achieve that effect

Then you can also map that:

:nnoremap <silent><Leader><C-]> <C-w><C-]><C-w>T

Edit: also, depending on what you actually want, don't forget you can open tags in preview (:ptag) with e.g. C-w}. Just mentioning it in case...

Solution 2 - Vim

Here are two pretty ad-hoc mappings (in case your tags are generated by ctags):

nnoremap <C-]> :tabnew %<CR>g<C-]>
vnoremap <C-]> <Esc>:tabnew %<CR>gvg<C-]>

First we open current buffer in a new tab; then we try to jump to a tag under cursor (g<C-]>, which is equal to :tjump, jumps to the tag directly if there's only one match, or provides a list of matches if there are many).

Pros:

Cons:

  • if you exit from list of matches without choosing any of them, the newly created tab will remain open
  • the same happens if there are no matches at all

P.S. Could you provide a use case for visual mode mapping?

P.P.S. If you generate tags with cscope (which is better than ctags) and use its vim mappings, replace the above mappings with the following ones:

nnoremap <C-]> :tabnew %<CR><C-]>
vnoremap <C-]> <Esc>tabnew %<CR>gv<C-]>

Solution 3 - Vim

In case somebody is still looking for a solution. On this solution when no tag is found no more blank tab will be left.

function! w:GoToTag(tagWord)

    let l:tagfile = &tags
    :tabe
    execute 'set tags=' . l:tagfile
    execute ':silent tjump ' . a:tagWord

    let l:tagFilename = expand('%:t')

    if l:tagFilename == ''
        :tabclose
        :tabprevious
    endif
endfunction

Solution 4 - Vim

You can set up a keyboard shortcut, 'g' followed by CONTROL-], in ~/.vimrc as follows:

nmap g<C-]> :execute 'tab tag '.expand('<cword>')<CR>

nmap       means 'when in normal mode'
g<C-j>     is the shortcut, 'g' followed by CTRL-]
execute    is a means of executing a command passed as a string
tab tag    means "open a new tab and run 'ta'"
expand     is used to expansion of a vim item
<cword>    means a word the same as used for '*'. See also <cWORD>

You can test "tab ta" via :tab tag functionname

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul TarjanView Question on Stackoverflow
Solution 1 - VimseheView Answer on Stackoverflow
Solution 2 - VimdorsergView Answer on Stackoverflow
Solution 3 - VimnotmiiView Answer on Stackoverflow
Solution 4 - VimMalcolm BoekhoffView Answer on Stackoverflow