68

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.

Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
  • 3
    possible duplicate of [Opening (c)tags in new tab in (G)Vim](http://stackoverflow.com/questions/908269/opening-ctags-in-new-tab-in-gvim) – richq May 20 '11 at 14:03
  • @richq I tried the top voted solution in my .vimrc and it didn't do anything when pressing ctrl+\. – Paul Tarjan May 23 '11 at 02:29
  • fair enough, I should have used http://stackoverflow.com/questions/539231 as the dupe, as that does contain sehe's answer too. I wonder if works for anything? It mixes Control and and probably just confuses vim. – richq May 23 '11 at 07:00
  • I use `` to toggle NERDTree. You can totally map `` :) – Justin Force Jun 10 '14 at 22:49

4 Answers4

110

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...

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    sadly, if the tag operation halts for any reason, this doesn't work. For example, if there are multiple tags and I have to pick between them. Any solution? – Paul Tarjan Jun 07 '11 at 07:32
  • @uɐɾɹɐʇ ןnɐd: that will depend on your definition of `` - with me it appears to always jump to the tag with highest prio without asking (as documented). Perhaps you have interfering mappings, see `:verbose map `, `:verbose imap ` etc – sehe Jun 07 '11 at 09:24
  • It breaks when the tag jump fails for some reason or another. Maybe there are multiple files I have to choose from, or the file is open in another buffer. Any ideas how to deal with that? – Paul Tarjan Jul 10 '11 at 07:34
  • @uɐɾɹɐʇ ןnɐd: what I said in my previous comment... [docs](http://vimdoc.sourceforge.net/htmldoc/tagsrch.html#CTRL-]); Did you try mapping ex commands, instead of `normal mode` commands? [You could use `:stag!`](http://vimdoc.sourceforge.net/htmldoc/tagsrch.html#tag-!) instead, [or even `:silent! stag!`](http://vimdoc.sourceforge.net/htmldoc/various.html#:silent). Tip: Sneak in ex commands in a `insert mode` mapping [using ``](http://vimdoc.sourceforge.net/htmldoc/insert.html#i_CTRL-O) – sehe Jul 10 '11 at 23:08
  • :nnoremap T is not working for me so tried :nnoremap T its working but the arrow keys misbehaves can you help me on this – Dinesh May 14 '15 at 12:07
  • @PaulTarjan What about having the mapping press gT instead? That solves the problem if there are multiple tags, but not if there is no tag – Dylanthepiguy May 04 '18 at 10:22
  • This is awesome. One issue I found was that you jump to a new tab no matter what, even if the definition is in the current tab. Is there a way around that? – Jake Byman Feb 08 '19 at 00:34
  • @JakeByman not readily. Perhaps `:tj` with `:se switchbuf=useopen,usetab` could be a start (but I haven't tried. I use YouCompleteMe for 90% navigation of navigation tasks, and when jumping inside a file will just use `C-]`) – sehe Feb 08 '19 at 00:41
  • 1
    Boom! The `set switchbuf=useopen/usetab` did the trick! You're my favorite, this is gamechanging – Jake Byman Feb 08 '19 at 00:49
  • It's a combination of two sequences, `:help CTRL-W_]` and `:help CTRL-W_T`. – Weekend Nov 14 '19 at 09:07
  • This mapping freezes my vim. – hasufell Oct 11 '20 at 16:45
6

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-]>
Community
  • 1
  • 1
dorserg
  • 5,454
  • 3
  • 24
  • 29
  • For visual mode mapping, our codebase has some classes in them named `foo:bar:baz` but my ctags breaks on `:` since I don't want to select `foo::bar` as one entity. So I usually go into visual mode over `foo:bar:baz` to jump to it. – Paul Tarjan Jul 30 '11 at 05:08
  • I see. So have these mappings helped? The commands used in them are pretty general, so hopefully they work everywhere without conflicts. – dorserg Aug 02 '11 at 12:23
3

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
notmii
  • 413
  • 4
  • 9
3

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

Malcolm Boekhoff
  • 1,032
  • 11
  • 9
  • 1
    I tried everything...only solution that worked with NERDTreeTabsToggle. Thank you!!!!! – g.o.a.t. Jul 12 '19 at 21:24
  • And if you use `~/.vim` instead of `~/.vimrc` then put the line in a file called `~/.vim/plugin/ctags.vim` or similar. – maikel Jan 10 '20 at 04:24