How to jump next tag when using tagbar in VIM

4k views Asked by At

when using VIM with tagbar displayed. I want to use the next command in .vimrc

nnoremap tn <C-w>h<cr>

to jump to the next tag in the tagbar. But the it takes no effect at all.

when i typed tn, the cusor is jumped to the next tag, but the main window keeps still

is there somebody can help me?

2

There are 2 answers

6
Ingo Karkat On BEST ANSWER

Normally, <CR> in normal mode just moves the cursor to the beginning of the next line. To make that key jump to the tag on the current line, the tagbar plugin defines a buffer-local mapping in its tag window. Though it's generally recommended to use :nnoremap, this is one case where you have to allow remappings by using :nmap:

:nmap tn <C-w>h<cr>
4
romainl On

I don't use TagBar anymore so I can't test it for you but the doc seems to say that you can use:

:TagbarOpen fj

to jump to the TagBar window. I think it is a little more solid than <C-w>h which is pretty much tied to a specific layout.

Thus, a possible mapping would be:

nnoremap tn :TagbarOpen fj<CR>

But neither your mapping nor this one allow you to place the cursor on the next tag which is what you seem to want ("to jump to the next tag in the tagbar"). TagBar has a mapping, <C-n> to go to the next top-level tag which may or may be not what you want. You could add it to the mix or, maybe, use j:

nnoremap tn :TagbarOpen fj<CR><C-n>
nnoremap tn :TagbarOpen fj<CR>j

As a side note, after a month of usage, I found TagBar to be rather good at showing the structure of my code but rather bad at helping me navigate through it. Because I used it primarily for navigation, I ditched it for Vim's default ctags integration (<C-]>, :tag <Tab>, :tselect and so on…) and CtrlP's wonderful :CtrlPBufTag and :CtrlPTag.