Exuberant Ctagsでタグファイル作ってVimでタグジャンプしたら便利だった
今まで良く分からずスルーしてたctagsとかの解説を、ちゃんと読んでみました。どうやら、タグジャンプという機能が、Vimにあったらなぁと今まで思っていた機能のようなので、早速導入してみました。タグジャンプは、ざっくりと言えば関数やメソッドの宣言部分を開いて移動する機能です。eclipseならF3キー。ジャンプできるかどうかは、タグファイルを生成できる言語かどうかによるみたいだけど。
Macで導入する
$ ctags usage: ctags [-BFadtuwvx] [-f tagsfile] file ... $ etags -V etags (GNU Emacs 22.1) Copyright (C) 2007 Free Software Foundation, Inc. This program is distributed under the terms in ETAGS.README
Macに最初から入ってるctags
は役に立たない。バージョンを表示するオプションすらないらしい。調べてみるとBSD系のもので、C言語専用みたいなものなんだとか。etags
というのもあるけど、emacs系のものでディレクトリを再帰的に処理するオプションが無いらしい。
MacPortsでExuberant Ctagsを入れる
$ port info ctags ctags @5.8 (devel) Variants: universal Description: Exuberant Ctags generates an index (or tag) file of source language objects in source files that allows these items to be quickly and easily located by a text editor or other utility. Alternatively, it can generate a cross reference file which lists, in human-readable form, information about the various objects found in a set of source code files. Supported languages include: Assembler, AWK, ASP, BETA, Bourne/Korn/Zsh Shell, C, C++, COBOL, Eiffel, Fortran, Java, Lisp, Lua, Make, Pascal, Perl, PHP, Python, REXX, Ruby, S-Lang, Scheme, Tcl, Vim and Yacc. Homepage: http://ctags.sourceforge.net/ Platforms: darwin, freebsd License: GPL-2 Maintainers: mww@macports.org, openmaintainer@macports.org $ sudo port install ctags
Exuberant CtagsがVimと共に使われているもので、いろんな言語のタグファイルを作れるものらしい。
type ctags $ type ctags ctags is /usr/bin/ctags #ターミナルのウィンドウを開きなおしたり、ターミナルを再起動とか $ type ctags ctags is hashed (/opt/local/bin/ctags)
コマンドはctags
なので、入れたらターミナルのウィンドウを新しく開きなおそう。*1
使う
$ cd /path/to/cakephp $ ls README app index.php lib plugins vendors $ ctags -R $ ls README index.php plugins vendors app lib tags $ vim lib/Cake/Console/Shell.php #タグジャンプが出来る
例えばCakePHPのソースコードのタグファイルを作る。-R
はディレクトリ内を再帰的に処理してタグファイルを作っていく。これでCakePHPのソースコード内ならタグジャンプが使えるようになりました。
タグファイルを何処から読み込むか設定する
$ cd lib $ vim Cake/Console/Shell.php #タグジャンプが使えない
:set tags tags=./tags,tags
設定 | 意味 |
./tags | 開いているファイルと同じ階層にあるtags |
tags | カレントディレクトリにあるtags |
Vimの初期設定ではこのようになっています。この設定だと、上記のようにタグファイルのある階層より下に移動していて、タグファイルのある階層より下のファイルを開いていると、タグジャンプが出来ません。
if has('path_extra') set tags+=tags;/Users/myuser endif
基本的に自分のホーム以上に作ったプログラムを置く事も無いので、カレントディレクトリからホームディレクトリまでにあるtags
を読み込む設定を追加しました。path_extra
が使えるならだけど、上記の問題が解決します。
設定 | 意味 |
tags; | カレントからtags が見つかるまで、ディレクトリを上に辿りながら探す |
tags;/Users/myuser | 上記と同じくだけど、/Users/myuser より上には行かない |
ただ、どう考えても設定はあってるはずなんですが、自分の環境ではホームを越えて/tags
を読み込んじゃってます。まぁ、探さなくても良いとこでtags
を探す処理が走るだけなので、体感で分からないくらいのちょっと長い起動時間になる程度の問題だと思います。
- http://vim-users.jp/2010/06/hack154/
- https://sites.google.com/site/vimdocja/editing-html#file-searching
set tags
のいろんな設定や、path_extra
を使ったファイル検索の動きについては、この辺を参考にしました。