Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: Open tag in new tab

Tags:

vim

vim-plugin

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.

like image 383
Paul Tarjan Avatar asked May 20 '11 08:05

Paul Tarjan


People also ask

How do I open a new tab in Vim?

Opening a tab Probably the easiest to remember is to run the :tabnew command while in normal mode. This will open a new tab with an empty buffer. If you want to edit a file in the new tab, you can run :tabnew filename and Vim will load the file in the new tab.

How do I switch between tabs in Vim?

To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast).

How do I use tabs in Vim?

To directly move to first tab or last tab, you can enter the following in command mode: :tabfirst or :tablast for first or last tab respectively. To move back and forth : :tabn for next tab and :tabp for previous tab. You can list all the open tabs using : :tabs. To open multiple files in tabs: $ vim -p source.

How do I open multiple tabs in Vim?

Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l ) to navigate through open windows. Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one. Starting vim with a -o or -O flag opens each file in its own split.


2 Answers

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

like image 177
sehe Avatar answered Sep 21 '22 12:09

sehe


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:

  • "works on my machine" ©

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-]> 
like image 21
dorserg Avatar answered Sep 21 '22 12:09

dorserg