vim:在新选项卡中打开标签

发布于 2024-11-08 14:42:00 字数 142 浏览 4 评论 0原文

是否有插件或脚本可以在新选项卡中打开 ctags 条目?我想将光标放在某个函数上,按 ctrl+] 并在另一个选项卡中打开该条目。我还希望如果我直观地选择一个条目,则 ctrl+] 仍然可以工作并在新的 vim 选项卡中打开。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时常饿 2024-11-15 14:42:00

您可以

CwC-]CwT

来实现该效果

然后您还可以映射该效果:

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

编辑: 另外,根据您实际想要的内容,不要忘记您可以使用例如在预览中打开标签(:ptagCW}。只是提一下以防万一...

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

风吹雪碎 2024-11-15 14:42:00

这里有两个漂亮的临时映射(如果您的标签是由 ctags 生成的):

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

首先,我们在新选项卡中打开当前缓冲区;然后我们尝试跳转到光标下的某个标签(g,相当于:tjump,如果只有一个匹配则直接跳转到该标签,或者提供匹配列表(如果有很多)。

优点:

缺点:

  • 如果您从匹配列表中退出如果不选择其中任何一个,新创建的选项卡将保持打开状态,
  • 如果根本没有匹配项,也会发生同样的

情况您能否提供视觉模式映射的用例?

PPS 如果您使用 cscope 生成标签(比 ctags 更好)并使用其 vim 映射,将上面的映射替换为以下映射:

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

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-]>
水水月牙 2024-11-15 14:42:00

万一有人仍在寻找解决方案。在此解决方案中,当未找到标签时,将不再留下空白选项卡。

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

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
烟雨凡馨 2024-11-15 14:42:00

您可以在 ~/.vimrc 中设置键盘快捷键,'g' 后跟 CONTROL-],如下所示:

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>

您可以通过 :tab 标签 functionname 测试“tab ta”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文