ack.vimで編集効率を10倍向上させる
Vimテクニックバイブル ~作業効率をカイゼンする150の技
ここ1週間、Vimテクニックバイブルに載ってる使ったことのないプラグインを試しているんですが、個人的にvimgrepよりも使いやすいと思っているack.vimが見当たらなかったのでご紹介。
ackとは
ack 1.96 — better than grep, a source code search tool for programmers
findで再帰的に全ファイルを列挙して、各ファイルに対してgrepというような事を一つのコマンドでやってくれます。.svnとかCVSとかを検索対象として自動で除外してくれるので、ソースコードの検索に非常に役に立ちます。findとgrepでソース検索してたら、「それack使うと楽だよ」と会社の親切な人が教えてくれましたよ。基本的な利用方法は下記のような感じで
ack パターンマッチ [ファイル名(省略可)] |
CentOSとかならyumで入ります(要epel)
yum -y install ack |
Macでhomebrew使ってるなら
brew install ack |
ack.vimとは
ack.vimは、vim上からackを呼び出して、検索結果をquickfixに一覧表示してくれる、素晴らしいプラグインです。ack.vimを使ってるか、使ってないかで編集効率が数倍違います。
ack.vimの導入
ackを呼び出しているので、ackが利用出来る必要がありますのであらかじめサーバにいれておきましょう。
ack.vimはVundleを使ってれば、.vimrcに
Bundle 'ack.vim' |
とかいて、:BundleInstallするだけです。
またmileszs/ack.vim – GitHubに書いてあるように、GitHubからもってきて、rake installでも導入可能です。
git clone https://github.com/mileszs/ack.vim cd ack.vim rake install |
今風(モダン)なやり方では無いですが、直接ダウンロードしてきても利用出来ると思います。
curl -o ack.tar.gz "http://www.vim.org/scripts/download_script.php?src_id=10433" mv ack.tar.gz ~/.vim cd ~/.vim tar xvzf ack.tar.gz |
ack.vimの使い方
実際の使い方は、非常にシンプルで下記のような感じです。
# 基本的な使い方 :Ack [options] {pattern} [{directory}] # hogeという文字を検索するときは :Ack hoge |
コマンドのackと使い方は同じですし、オプションがそのまま利用出来るので、vimgrepのように直感的でない引数を覚える必要がありません。
ack.vim便利す。
*ack.txt* Plugin that integrates ack with Vim
==============================================================================
Author: Antoine Imbert <antoine.imbert+ackvim@gmail.com> *ack-author*
License: Same terms as Vim itself (see |license|)
==============================================================================
INTRODUCTION *ack*
This plugin is a front for the Perl module App::Ack. Ack can be used as a
replacement for grep. This plugin will allow you to run ack from vim, and
shows the results in a split window.
:Ack[!] [options] {pattern} [{directory}] *:Ack*
Search recursively in {directory} (which defaults to the current
directory) for the {pattern}. Behaves just like the |:grep| command, but
will open the |Quickfix| window for you. If [!] is not given the first
error is jumped to.
:AckAdd [options] {pattern} [{directory}] *:AckAdd*
Just like |:Ack|, but instead of making a new list, the matches are
appended to the current |quickfix| list.
:AckFromSearch [{directory}] *:AckFromSearch*
Just like |:Ack| but the pattern is from previous search.
:LAck [options] {pattern} [{directory}] *:LAck*
Just like |:Ack| but instead of the |quickfix| list, matches are placed in
the current |location-list|.
:LAckAdd [options] {pattern} [{directory}] *:LAckAdd*
Just like |:AckAdd| but instead of the |quickfix| list, matches are added
to the current |location-list|
:AckFile [options] {pattern} [{directory}] *:AckFile*
Search recursively in {directory} (which defaults to the current
directory) for filenames matching the {pattern}. Behaves just like the
|:grep| command, but will open the |Quickfix| window for you.
Files containing the search term will be listed in the split window, along
with the line number of the occurrence, once for each occurrence. <Enter> on
a line in this window will open the file, and place the cursor on the matching
line.
See http://betterthangrep.com/ for more information. |
Vimテクニックバイブル ~作業効率をカイゼンする150の技
著者/訳者:Vimサポーターズ
出版社:技術評論社( 2011-09-23 )
定価:¥ 3,129
Amazon価格:¥ 3,129
単行本(ソフトカバー) ( 384 ページ )
ISBN-10 : 4774147958
ISBN-13 : 9784774147956
Subversionでログメッセージ(コメント)の入力を強制する
私は普段git-svnを利用しているので、意識せずコミットの際コメントを入れる癖が付いてるんですが、社内でSubversionを利用していて、ログメッセージ(コメント)の無いコミットが問題になったので、ログメッセージの無いコミットを拒否するように設定したのでメモ。
cd path_to_dir/hooks sudo wget http://www.powertrip.co.za/blog/archives/pre-commit sudo cp pre-commit{,.dist} sudo chown apache.apache pre-commit sudo chmod +x pre-commit |
所有者は同じディレクトリ内のファイルと合わせてください。
ダウンロードしてきた修正前のpre-commitのソースは下記のような感じです。
#!/usr/local/bin/python """ Subversion pre-commit hook which currently checks that the commit contains a commit message to avoid commiting empty changesets which tortoisesvn seems to have a habbit of committing. Based on http://svn.collab.net/repos/svn/branches/1.2.x/contrib/hook-scripts/commit-block-joke.py and hooks/pre-commit.tmpl Hacked together by Jacques Marneweck <jacques@php.net> $Id$ """ import sys, os, string SVNLOOK='/usr/local/bin/svnlook' def main(repos, txn): log_cmd = '%s log -t "%s" "%s"' % (SVNLOOK, txn, repos) log_msg = os.popen(log_cmd, 'r').readline().rstrip('\n') if len(log_msg) < 10: sys.stderr.write ("Please enter a commit message which details what has changed during this commit.\n") sys.exit(1) else: sys.exit(0) if __name__ == '__main__': if len(sys.argv) < 3: sys.stderr.write("Usage: %s REPOS TXN\n" % (sys.argv[0])) else: main(sys.argv[1], sys.argv[2]) |
pythonとsvnlookのpathを調べて、サーバに合わせるようにvimとかでスクリプトを修正。
which python /usr/bin/python which svnlook /usr/bin/svnlook |
diff -u pre-commit{,.dist} --- pre-commit 2011-09-29 11:09:16.000000000 +0900 +++ pre-commit.dist 2011-09-29 11:42:53.000000000 +0900 @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/local/bin/python """ Subversion pre-commit hook which currently checks that the commit contains a commit message to avoid commiting empty changesets which tortoisesvn seems @@ -14,7 +14,7 @@ import sys, os, string -SVNLOOK='/usr/bin/svnlook' +SVNLOOK='/usr/local/bin/svnlook' def main(repos, txn): log_cmd = '%s log -t "%s" "%s"' % (SVNLOOK, txn, repos) |
tmplという拡張子にしないと動かないのかと思いましたが、pre-commitのままで動作しました。
pre-commit.tmplとpre-commitとがそれぞれある状態です。
muninの利用可能プラグインを一括で有効にする
munin-node-configure を使うと、利用可能なプラグインを一斉に有効に出来るんですが、いつも忘れるのでメモ。利用環境はCentOS 5.6です。
下記コマンドで、lnコマンドを確認。
munin-node-configure --suggest --shell --families auto ln -s '/usr/share/munin/plugins/munin_stats' '/etc/munin/plugins/munin_stats' ln -s '/usr/share/munin/plugins/nfs4_client' '/etc/munin/plugins/nfs4_client' ln -s '/usr/share/munin/plugins/ntp_kernel_err' '/etc/munin/plugins/ntp_kernel_err' ln -s '/usr/share/munin/plugins/ntp_kernel_pll_freq' '/etc/munin/plugins/ntp_kernel_pll_freq' ln -s '/usr/share/munin/plugins/ntp_kernel_pll_off' '/etc/munin/plugins/ntp_kernel_pll_off' ln -s '/usr/share/munin/plugins/ntp_offset' '/etc/munin/plugins/ntp_offset' |
パイプでshに渡して、実行
munin-node-configure --suggest --shell --families auto | sh |
munin-nodeを再起動で設定反映。
service munin-node restart |
ちなみに新規プラグインを用意しても動かない場合は、
munin-node-configure --suggest |
すると原因が分かったりします。
colordiffでdiff結果に色をつける
git diffはカラー表示されてるのに、通常のdiffはカラー表示されず、見づらいなーと思っていたらcolordiffという色付けしてくれるツールがありました。(もしかして有名なのか!)
導入環境はCentOS 5.6です。
sudo yum -y install colordiff |
Macでhomebrew使ってるなら
brew install colordiff |
.bashrcや.zshrcに追加して、ノーマルdiffを置き換え
alias diff='colordiff' |
colordiffの結果をパイプでlessとかに渡すとおかしなことになるので、-Rを付けるとちゃんとカラー表示される。
colordiff hogemoge.conf{,.dist} | less -R |
毎回 -R とか付けると面倒なので、これも.bashrcや.zshrcに書いておく。
export LESS="=R" |
パッチファイルも
colordiff -c hoge.conf{,.dist} > hoge.patch |
とかでちゃんとノーマルdiffと同じようにpatchファイルも作られるので、問題無さそうです。






