Gnu Global, the programmer’s friend

GNU Global (gtags) is a source code tagging system similar to ctags but more than that. I always use vim as my c/c++ source code editor, and using ctags to helps me travels from function call to function definition. I do write a post indicateshow does ctags works with vim.

The reason I shifted to gtags is because ctags is no longer sufficient to me. I have to trace the codes from large volume than last time, and they scatter into many different folders.

Gtags provides me not only travels to definition of the function calls, but also allows me to locate the function calls from the definition, the reverse way. To be precise, it capable of locate objectdefinitions, objectreferences, both of them based onpatterns I specified.

Gtags works with vim too, by doing some keymaping tweaks, it works the same like ctags do, and more.

To get gnu global install to your system, check out the official website’s download page.

After the installation, goto your root path of your c/c++ project, and trigger this and go make a coffee.

gtags

Gtags will pickup whatever needed and create FOUR tagging db file, refers here for more info.

In order to allow Gtags working in vim, you have to copy over the gtags.vim into vim plugin’s folder. ( Refers to here on how to install vim’s plugin script.)

Vim with gtags
For vim with ctags, hitting Ctrl+] will jump you to object definition, I gonna do the same for vim with gtags. Besides that, I gonna mapCtrl+\ to list all the object references.

Now, lets us edit vimrc, copy and paste those lines into your vimrc.

"gtags
:map <C-]> :Gtags<CR><CR>
:map <C-\> :Gtags -r<CR><CR>
:map <F10> :cclose<CR>
:map <F11> :cp<CR>
:map <F12> :cn<CR>

*nix, vimrc is at ~/.vimrc
windows gvim, Edit > Startup Settings

When Gtags list all the object references, it will open a split windows, to travels from one to another, use :cn and :cp, to close the split windows use :cclose, all are mapped to F11, F12 and F10.

You can find your codes in your shell too, by using command global.

To list all definitions of “GetExecute”, you do this:

global GetExecute

For more details listing, you can add in -x option, which it will print out the line as well as the line number.

global -x GetExecute

To list all the references of “GetExamples”, you do this:

global -rx GetExamples

You can add in your regex in your search such as *, see this:

global -rx GetLinuxBy*

3.5 Vim using GLOBAL

In Vim 6.2 or later, you can use the ‘gtags.vim’ script.


[ << ][ < ][ Up ][ > ][ >> ]    [Top][Contents][Index][ ? ]

3.5.1 Features

  • You can use most of GLOBAL’s facilities from Vim.
  • Intelligent recognition of the current token and its type.
  • Special characters ‘%’, ‘#’ and input completion are available.

To our regret, the tag stack facility is not available.If you want to use the facility, please try gtags-cscope.See section Gtags-cscope.


[ << ][ < ][ Up ][ > ][ >> ]    [Top][Contents][Index][ ? ]

3.5.2 Preparation

First, do the preparation of global. See section Preparation.

Second, copy ‘gtags.vim’ to your plug-in directory or source itfrom your vimrc.

    $ cp /usr/local/share/gtags/gtags.vim $HOME/.vim/plugin

[ << ][ < ][ Up ][ > ][ >> ]    [Top][Contents][Index][ ? ]

3.5.3 Usage

  • To go to main, you can say
        :Gtags main
    

    Vim executes global(1), parses the output, lists located tagsin quickfix window and loads the first entry.The quickfix window is like this:

    gozilla/gozilla.c|200| main(int argc, char **argv)
    gtags-cscope/gtags-cscope.c|124| main(int argc, char **argv)
    gtags-parser/asm_scan.c|2056| int main()
    gtags-parser/gctags.c|157| main(int argc, char **argv)
    gtags-parser/php.c|2116| int main()
    gtags/gtags.c|152| main(int argc, char **argv)
    [Quickfix List]
    

    You can go to any entry using quickfix command.

    :cn

    go to the next entry.

    :cp

    go to the previous entry.

    :ccN

    go to the N’th entry.

    :cl

    list all entries.

    You can see the help of quickfix like this:

        :h quickfix
    

    Suggested map:

        map <C-n> :cn<CR>
        map <C-p> :cp<CR>
    
  • To go to the referenced point of func1, add the ‘-r’ option.
        :Gtags -r func1
    
  • To locate symbols which are not defined in ‘GTAGS’, try this:
        :Gtags -s lbolt
    
  • To locate strings, try this:
        :Gtags -g int argc
    
        :Gtags -g "root"
    
        :Gtags -ge -C               <- locate '-C'
    
  • To get a list of tags in specified files, use the ‘-f’ command.
        :Gtags -f main.c            <- locate tags in main.c
    

    If you are editing ‘main.c’ itself, you can use ‘%’ instead.

        :Gtags -f %                 <- locate tags in main.c
    
  • You can use POSIX regular expressions.
        :Gtags ^put_              <- locate tags starting with 'put_'
        
        :Gtags -g fseek(.*SEEK_SET) <- locate fseek() using SEEK_SET
    
  • Input completion is available.

    In the command line, press CTRL-D after some typingand Vim will show a list of tag names that start with the string.Press <TAB> and Vim will complete the tag name.

        :Gtags fu<TAB>
    
        :Gtags func1                <- 'nc1' is appended by vim
    
  • You can browse files whose path includes specified pattern.
        :Gtags -P /vm/              <- under vm/ directory
        :Gtags -P \.h$              <- all include files
        :Gtags -P init              <- path including 'init'
    
  • You can use all options of global(1) except for ‘-c -n -p -q -u -v’ andall long name options. They are sent to global(1) as is.For example:
        :Gtags -gi paTtern        <- matches both 'PATTERN' and 'pattern'
    
        :Gtags -POi make          <- matches Makefile but not makeit.c
    

    About the other options, please see global - print locations of given symbols.

  • The GtagsCursor command brings you to the definition or reference of the current token.

    If the context of the current token is a definition then it is equivalent to:Gtags -r current-token;if it is a reference to some definitions then it is equivalent to:Gtags current-token; else it is equivalentto :Gtags -s current-token.

        :GtagsCursor
    

    Suggested map:

        map <C-\>^] :GtagsCursor<CR>
    

    Though the mapping of :GtagsCursor to ^] seems suitable,it will bring an inconvenience in the help screen.

  • If you have the hypertext generated by htags(1) then you can displaythe same part of the source code in the mozilla browser.Let’s load mozilla and try this:
        :Gozilla
    

    Suggested map:

        map <C-g> :Gozilla<CR>
    
  • If you want to load Vim with all main()s then following command line is useful.
        $ vim '+Gtags main'
    

You can search by symbol as well as grep too.

global -sx GetLinuxBy*
global -gx GetLinuxBy*

And more.

Vim中Gtags操作的标注格式是:

 

:Gtags [option] pattern

  • function:  :Gtags func<TAB>
  • 支持标准POSIX正则式: :Gtags ^[sg]et_
  • function reference: :Gtags -r func
  • 符号(非全局定义): :Gtags -s func
  • 普通字符串::Gtags -g xxx
  • 文件中定义的数据::Gtags -f filename/%(当前文件)
  • 文件浏览: :Gtags -P regular
  • 跳转到定义位置::GtagsCursor

Quickfix list操作:

  • :cn'  下一个
  • :cp' 上一个
  • :ccN' 去第N个
  • :cl' 显示全部

Thanks to Shigio YAMAGUCHI, Hideki IWAMOTO et al. for such wonderful tools and hope you enjoy coding with global.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值