Vim 和 Ctags 提示和技巧

发布于 2024-07-14 01:04:26 字数 374 浏览 9 评论 0原文

我刚刚使用我的 Vim(或者更确切地说 gVim)安装了 Ctags (以帮助 C++ 开发),并想找出您最喜欢的命令、宏、快捷方式、与之相关的提示...

分享您最好的武器库。 您会推荐哪些其他 Vim 插件用于 Vim 上的 C++ 开发?

编辑您还会将哪些其他附加组件与 Ctags 结合使用?

EDIT2 您使用标签的 gVim 版本是什么? 这有什么不同吗?

EDIT3 您如何增强大型和小型项目的编程经验?

I have just installed Ctags (to help with C++ development) with my Vim (or rather gVim), and would like to find out your favorite commands, macros, shortcuts, tips that go along with it...

Share your best arsenal. What other Vim add-ons you would recommend for C++ on Vim development?

EDIT What other add-on you would use in conjunction with Ctags?

EDIT2 What version of gVim you use with tags? Does it make a difference?

EDIT3 How do you enhance your programming experience for both big and small projects?

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

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

发布评论

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

评论(15

贩梦商人 2024-07-21 01:04:27

我改编了上面的 SetTags() 搜索函数(应将其替换为等效的 settags+=./tags;/)以适用于 cscope。 似乎有效!

"cscope file-searching alternative
function SetCscope()
    let curdir = getcwd()

    while !filereadable("cscope.out") && getcwd() != "/"
            cd ..
                endwhile

    if filereadable("cscope.out")
            execute "cs add " . getcwd() . "/cscope.out"
                endif

    execute "cd " . curdir
    endfunction

call SetCscope()

I adapted the SetTags() search function above (which should be replaced by the equivalent set tags+=./tags;/) to work for cscope. Seems to work!

"cscope file-searching alternative
function SetCscope()
    let curdir = getcwd()

    while !filereadable("cscope.out") && getcwd() != "/"
            cd ..
                endwhile

    if filereadable("cscope.out")
            execute "cs add " . getcwd() . "/cscope.out"
                endif

    execute "cd " . curdir
    endfunction

call SetCscope()
关于从前 2024-07-21 01:04:27

上面 SetCscope() 函数的另一个迭代。 这将设置 cscope 预路径来获取匹配项,而无需位于“cscope.out”所在的目录上:

function s:FindFile(file)
    let curdir = getcwd()
    let found = curdir
    while !filereadable(a:file) && found != "/"
        cd ..
        let found = getcwd()
    endwhile
    execute "cd " . curdir
    return found
endfunction

if has('cscope')    
    let $CSCOPE_DIR=s:FindFile("cscope.out")
    let $CSCOPE_DB=$CSCOPE_DIR."/cscope.out"
    if filereadable($CSCOPE_DB)
        cscope add $CSCOPE_DB $CSCOPE_DIR
    endif

    command -nargs=0 Cscope !cscope -ub -R &
endif

Another iteration on the SetCscope() function above. That sets cscope pre-path to get matches without being on the dir where "cscope.out" is:

function s:FindFile(file)
    let curdir = getcwd()
    let found = curdir
    while !filereadable(a:file) && found != "/"
        cd ..
        let found = getcwd()
    endwhile
    execute "cd " . curdir
    return found
endfunction

if has('cscope')    
    let $CSCOPE_DIR=s:FindFile("cscope.out")
    let $CSCOPE_DB=$CSCOPE_DIR."/cscope.out"
    if filereadable($CSCOPE_DB)
        cscope add $CSCOPE_DB $CSCOPE_DIR
    endif

    command -nargs=0 Cscope !cscope -ub -R &
endif
草莓味的萝莉 2024-07-21 01:04:26

Ctrl+] - 转到定义
Ctrl+T - 从定义跳回。
Ctrl+W Ctrl+] - 以水平分割打开定义

在 vi​​mrc 中添加这些行
地图> :tab split:exec("tag ".expand(""))
地图:vsp:exec("tag ".expand(""))

Ctrl+\ - 打开新选项卡中的定义
以垂直分割方式打开定义

Alt+] -生成标签后 。 您可以使用以下键标记进入和标记出函数:

Ctrl+鼠标左键单击 - 转到定义
Ctrl+鼠标右键单击 - 从定义跳回

Ctrl+] - go to definition
Ctrl+T - Jump back from the definition.
Ctrl+W Ctrl+] - Open the definition in a horizontal split

Add these lines in vimrc
map <C-\> :tab split<CR>:exec("tag ".expand("<cword>"))<CR>
map <A-]> :vsp <CR>:exec("tag ".expand("<cword>"))<CR>

Ctrl+\ - Open the definition in a new tab
Alt+] - Open the definition in a vertical split

After the tags are generated. You can use the following keys to tag into and tag out of functions:

Ctrl+Left MouseClick - Go to definition
Ctrl+Right MouseClick - Jump back from definition

千纸鹤 2024-07-21 01:04:26

我的 .vimrc 中总是有一行:

set tags=./tags;/

这将在当前目录中查找“标签”,并沿着树向根方向向上移动,直到找到一个。 IOW,您可以位于源树中的任何位置,而不仅仅是它的根。

One line that always goes in my .vimrc:

set tags=./tags;/

This will look in the current directory for "tags", and work up the tree towards root until one is found. IOW, you can be anywhere in your source tree instead of just the root of it.

陈甜 2024-07-21 01:04:26

另一个有用的 C 开发插件是 cscope
正如 Ctags 让您跳转到定义一样,Cscope 跳转到调用函数。

如果 ~/bin/ 目录中有 cscope,请将以下内容添加到 .vimrc 并使用 g^​​] 转到调用函数(请参阅 :help cscope)。

if has("cscope")
    set csprg=~/bin/cscope
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
        " else add database pointed to by environment
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
endif

差点忘了...就像 ctags - 您必须生成(并定期更新)数据库。 我使用以下脚本

select_files > cscope.files
ctags -L cscope.files
ctags -e -L cscope.files
cscope -ub -i cscope.files

,其中“select_files”是另一个从 Makefile 中提取 C 和头文件列表的脚本。 这样我只索引项目实际使用的文件。

Another useful plugin for C development is cscope
Just as Ctags lets you jump to definitions, Cscope jumps to the calling functions.

If you have cscope in your ~/bin/ directory, add the following to your .vimrc and use g^] to go to the calling function (see :help cscope).

if has("cscope")
    set csprg=~/bin/cscope
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
        " else add database pointed to by environment
    elseif $CSCOPE_DB != ""
        cs add $CSCOPE_DB
    endif
endif

Almost forgot... Just as ctags - you have to generate (and periodically update) the database. I use the following script

select_files > cscope.files
ctags -L cscope.files
ctags -e -L cscope.files
cscope -ub -i cscope.files

Where 'select_files' is another script that extracts the list of C and header files from the Makefile. This way I index only the files actually used by the project.

残疾 2024-07-21 01:04:26

您可以将目录添加到 ctags 查找中。 例如,我有一个为 Qt4 构建的 ctags 索引,并将其放在我的 .vimrc 中:

set tags+=/usr/local/share/ctags/qt4

You can add directories to your ctags lookup. For example, I have a ctags index built for Qt4, and have this in my .vimrc:

set tags+=/usr/local/share/ctags/qt4
独享拥抱 2024-07-21 01:04:26

以上所有内容以及...

code_complete :函数参数完整、代码片段等等。

taglist.vim :源代码浏览器(支持 C/C++、java、perl、python、tcl、sql、php 等)

All of the above and...

code_complete : function parameter complete, code snippets, and much more.

taglist.vim : Source code browser (supports C/C++, java, perl, python, tcl, sql, php, etc)

舂唻埖巳落 2024-07-21 01:04:26

我使用 ALT-left 和 ALT-right 从标签堆栈弹出/推送到标签堆栈。

" Alt-right/left to navigate forward/backward in the tags stack
map <M-Left> <C-T>
map <M-Right> <C-]>

如果您使用 hjkl 进行移动,则可以改为映射

I use ALT-left and ALT-right to pop/push from/to the tag stack.

" Alt-right/left to navigate forward/backward in the tags stack
map <M-Left> <C-T>
map <M-Right> <C-]>

If you use hjkl for movement you can map <M-h> and <M-l> instead.

感情旳空白 2024-07-21 01:04:26

同名的多个定义

g 以拆分方式打开定义,但也执行 :tjump它要么转到定义,要么如果有多个定义,则显示一个定义列表供您选择。

Several definitions of the same name

<C-w>g<C-]> open the definition in a split, but also do :tjump which either goes to the definition or, if there are several definitions, presents you with a list of definitions to choose from.

稀香 2024-07-21 01:04:26

我最常用的命令是 C-],它跳转到光标下函数的定义。 您可以更频繁地使用它来关注更多来电。 之后,Co 会将您带回一级,Ci 会再次深入。

The command I am using most is C-] which jumps to the definition of the function under the cursor. You can use it more often to follow more calls. After that, C-o will bring you back one level, C-i goes deeper again.

夜唯美灬不弃 2024-07-21 01:04:26

我发现 taglist 插件是必备的。 它在单独的窗口中列出了它知道的所有标签(您已打开的文件),并且可以非常轻松地导航较大的文件。

我主要将它用于 Python 开发,但对于 C/C++ 来说它只会更好。

I've found the taglist plug-in a must-have. It lists all tags that it knows about (files that you have opened) in a seperate window and makes it very easy to navigate larger files.

I use it mostly for Python development, but it can only be better for C/C++.

铃予 2024-07-21 01:04:26

我已将标签操作封装在 实验插件< /a> 我的。

关于vim中的C++开发,我已经回答了那里 :我使用自己的套件和一些其他插件。

I've encapsulated tags manipulation in an experimental plugin of mine.

Regarding C++ development in vim, I've already answered there: I use my own suite, and a few other plugins.

蝶舞 2024-07-21 01:04:26

两年来我一直在调整我的 vim 插件来支持足够大的 c++ 项目。 你可以看看它们。

他们使用 ctags 和 cscsope。

http://www.vim.org/scripts/script.php?script_id= 1638
http://www.vim.org/scripts/script.php?script_id=第2507章

I've been adapting my vim plugins for two years to support big enough c++ project. You can take a look at them.

They use ctags and cscsope.

http://www.vim.org/scripts/script.php?script_id=1638
http://www.vim.org/scripts/script.php?script_id=2507

夏の忆 2024-07-21 01:04:26

我将以下内容放入我的 .gvimrc 文件中,该文件在 gvim 启动时从任意点搜索树中的标签文件:

function SetTags()
    let curdir = getcwd()

    while !filereadable("tags") && getcwd() != "/"
        cd ..
    endwhile

    if filereadable("tags")
        execute "set tags=" . getcwd() . "/tags"
    endif

    execute "cd " . curdir
endfunction

call SetTags()

然后,我使用如下脚本定期在源树顶部重新生成标签文件:

#!/bin/bash

find . -regex ".*\.\(c\|h\|hpp\|cc\|cpp\)" -print | ctags --totals --recurse --extra="+qf" --fields="+i" -L -

I put the following in my .gvimrc file, which searches up the tree from any point for a tags file when gvim starts:

function SetTags()
    let curdir = getcwd()

    while !filereadable("tags") && getcwd() != "/"
        cd ..
    endwhile

    if filereadable("tags")
        execute "set tags=" . getcwd() . "/tags"
    endif

    execute "cd " . curdir
endfunction

call SetTags()

I then periodically regenerate a tags file at the top of my source tree with a script that looks like:

#!/bin/bash

find . -regex ".*\.\(c\|h\|hpp\|cc\|cpp\)" -print | ctags --totals --recurse --extra="+qf" --fields="+i" -L -
沦落红尘 2024-07-21 01:04:26

我在macos中使用vim,原来的ctags不能很好地工作,所以我下载最新的并配置make make install它。
我将 ctgas 安装在 /usr/local/bin/ctags 中(以保留原始版本)

"taglist
let Tlist_Ctags_Cmd = "/usr/local/bin/ctags"
let Tlist_WinWidth = 50
map <leader>ta :TlistToggle<cr>
map <leader>bta :!/usr/local/bin/ctags -R .<CR>
set tags=tags;/
map <M-j> <C-]>
map <M-k> <C-T>

I use vim in macos, and the original ctags doesn't work well, so I download newest and configure make make install it.
I install ctgas in /usr/local/bin/ctags(to keep original one)

"taglist
let Tlist_Ctags_Cmd = "/usr/local/bin/ctags"
let Tlist_WinWidth = 50
map <leader>ta :TlistToggle<cr>
map <leader>bta :!/usr/local/bin/ctags -R .<CR>
set tags=tags;/
map <M-j> <C-]>
map <M-k> <C-T>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文