http://blog.glidenote.com
に移転しました

Archive for "10月 2011"

社内で「モテるVimテクニック勉強会」を開催した

Vimテクニックバイブルが発売されて、社内のVim使いがにわかに活気づいていたので勉強会を開催しました。
単なる勉強会だと面白くないので、「モテるVimテクニック勉強会」として参加者を募ったところ、Vim使いはもちろん、Emacs使いや、普段Vimを使わない人など多くの人に参加頂けましたよ。

まずは論より証拠ということで、VimでFizzbuzzを書くとどんな感じになるかを実演。

上の動画でやってる作業を簡単に説明すると

  1. pythonファイルを新規作成すると自動で、#!/usr/bin/env python とか定型文が入る
  2. neocomplcacheのスニペット機能でfor文を展開
  3. smartchrで==の両端に自動でスペース挿入
  4. neocomplcacheのスニペット機能でif~elif~else文を展開
  5. if i % 5 == 0:とかは繰り返しなので、yankしてpaste
  6. 書き終わったら、quickrunでスクリプトを実行

Vimを使った上記のようなデモを7割、下記のような説明を3割という感じでした。

プラグインの活用と管理

最近Vimプラグインの開発は活発になっていて、新しいプラグインがどんどん出てきている。プラグインの管理の便利になってきってて、pathogenやvundle、neobundleなどを活用すると簡単にプラグインを導入できて管理も簡単でモテるよ。私はVundleを利用してます。

俺のオススメプラグイン

日常的によく利用しているプラグインを紹介。

  • unite.vim
  • neocomplcache
  • quickrun
  • sudo.vim
  • eregex.vim
  • Align
  • ref.vim
  • ack.vim
  • smartchr

他にも使っているプラグインあるけど、ここらへん押さえておくとモテるよ。

作業の効率化

レコーディングや自動整形といった標準機能と便利なプラグインを活用して作業を効率化しないとモテないよ。

.vimrcと.vimrc.localの使い分け

共通の設定は、.vimrc、マシン固有の特殊な設定は、.vimrc.localに書いて、.vimrcから読み込み。
分けることでMacだと動くけど、Linuxで動かないみたいな設定を回避出来るよ。
ちなみに私はOS毎だけでなく、仕事用と自宅作業用とで.vimrc.localの内容を分けてるよ

.vimrcをはじめdotfilesをgitで管理

git cloneでまっさらな環境でも2〜3分ですぐいつも使ってる自分の環境が構築出来るよ。
私のdotfilesは下記で管理しており、いつでもどのサーバでも利用出来るようにしてます。
glidenote/dotfiles – GitHub

Emacs使いと仲良くしよう

不毛な議論や意味のないことをしてるとモテないよ

ホームディレクトリに最新版を入れよう

サーバに入っているVimが古くてもホームディレクトリに最新版を入れれば利用出来るよ。自分の環境だけに入るから他の人に影響が出ないよ。

mkdir -p $HOME/local/{bin,src}
cd $HOME/local/src/
wget ftp://ftp.vim.org/pub/vim/unix/vim-7.3.tar.bz2
tar jxvf vim-7.3.tar.bz2
cd vim73
 
./configure \
--enable-multibyte \
--enable-xim \
--enable-fontset \
--with-features=big \
--prefix=$HOME/local
 
make
make install
$HOME/local/bin/vim

にvim73が入るので、aliasを振れば利用できますよ

echo "alias vim='$HOME/local/bin/vim'" >> $HOME/.bashrc
echo "alias vim='$HOME/local/bin/vim'" >> $HOME/.zshrc

仕事の道具には磨きをかけよう

エディタにかぎらずターミナル(bash,zsh,screen,tmux)に磨きをかけておくと仕事が楽しくなるし、モテるよ。

なかなか好評だったのとモテたいので、また機会があれば行いたいと思いますよ。

Vimテクニックバイブル ~作業効率をカイゼンする150の技

著者/訳者:Vimサポーターズ

出版社:技術評論社( 2011-09-23 )

定価:¥ 3,129

Amazon価格:¥ 3,129

単行本(ソフトカバー) ( 384 ページ )

ISBN-10 : 4774147958

ISBN-13 : 9784774147956


lsyncdで特定のファイルだけ同期する

lsyncd – Lsyncd (Live Syncing Daemon) synchronizes local directories with a remote targets – Google Project Hosting
lsyncdで特定のファイルだけ同期する方法があったのでメモ。
lsyncdはexcludeFromで同期除外(exclude)のファイル指定は可能なんですが、指定ファイルだけ同期(include)の設定が出来なかったので何とかならないものかと調べていたら、rsyncOps部分で実現出来ました。まあrsyncのオプションで無理矢理実現してるだけですが。導入完了はCentOS 5.7です。

yumでlsyncdの導入。

yum -y install lsyncd
rpm -ql lsyncd
 
/usr/bin/lsyncd
/usr/share/doc/lsyncd-2.0.4
/usr/share/doc/lsyncd-2.0.4/COPYING
/usr/share/doc/lsyncd-2.0.4/ChangeLog
/usr/share/doc/lsyncd-2.0.4/examples
/usr/share/doc/lsyncd-2.0.4/examples/lbash.lua
/usr/share/doc/lsyncd-2.0.4/examples/lecho.lua
/usr/share/doc/lsyncd-2.0.4/examples/lgforce.lua
/usr/share/doc/lsyncd-2.0.4/examples/limagemagic.lua
/usr/share/doc/lsyncd-2.0.4/examples/lpostcmd.lua
/usr/share/doc/lsyncd-2.0.4/examples/lrsync.lua
/usr/share/doc/lsyncd-2.0.4/examples/lrsyncssh.lua
/usr/share/man/man1/lsyncd.1.gz

yumで入れても設定ファイルも起動スクリプトも生成されないので自分で用意。

mkdir -p /etc/lsyncd/
 
/bin/cat<< EOF> /etc/lsyncd/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
        statusFile = "/tmp/lsyncd.stat",
        statusInterval = 1,
}
 
sync{
        default.rsync,
        source="/etc/",
        target="/root/tmp/etc/",
        rsyncOps={"-az", "--exclude-from=\/etc\/lsyncd\/rsync_exclude\.lst"},
}
EOF
 
/bin/cat<< EOF> /etc/lsyncd/rsync_exclude.lst
+ passwd
+ shadow
- *
EOF

上記設定は/etc/passwd,/etc/shadowだけ同期して、それ以外は同期しない設定になってます。

起動用スクリプト /etc/init.d/lsyncd を用意。ファイルの中身は下記のような感じ

#!/bin/bash
#
# chkconfig: - 85 15
# description: Lightweight inotify based sync daemon
#
# processname:  lsyncd
# config:       /etc/lsyncd/lsyncd.conf
# config:       /etc/sysconfig/lsyncd
# pidfile:      /var/run/lsyncd.pid
 
# Source function library
. /etc/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
OPTIONS="-pidfile /var/run/lsyncd.pid /etc/lsyncd/lsyncd.conf"
 
if [ -e /etc/sysconfig/lsyncd.options ]; then
  . /etc/sysconfig/lsyncd.options
fi
 
RETVAL=0
 
prog="lsyncd"
thelock=/var/lock/subsys/lsyncd
 
start() {
        echo -n $"Starting $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                daemon /usr/bin/lsyncd $OPTIONS
                RETVAL=$?
                [ $RETVAL -eq 0 ] && touch $thelock
        fi;
        echo
        return $RETVAL
}
 
stop() {
        echo -n $"Stopping $prog: "
        if [ $UID -ne 0 ]; then
                RETVAL=1
                failure
        else
                killproc /usr/bin/lsyncd
                RETVAL=$?
                [ $RETVAL -eq 0 ] && rm -f $thelock
        fi;
        echo
        return $RETVAL
}
 
reload(){
        echo -n $"Reloading $prog: "
        killproc /usr/bin/lsyncd -HUP
        RETVAL=$?
        echo
        return $RETVAL
}
 
restart(){
        stop
        start
}
 
condrestart(){
    [ -e $thelock ] && restart
    return 0
}
 
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  condrestart)
        condrestart
        ;;
  status)
        status lsyncd
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        RETVAL=1
esac
 
exit $RETVAL

同期先のディレクトリを作成

mkdir -p /root/tmp/etc/

起動スクリプトの登録と自動起動の設定

chmod u+x /etc/init.d/lsyncd
chkconfig --add lsyncd
chkconfig lsyncd on
/etc/init.d/lsyncd start

passwdとshadowを更新して、反映されているか確認。

useradd hoge
passwd hoge
more /root/tmp/etc/*

lsyncd.confで同期先をリモートサーバにすれば、リモートサーバと同期できて (゚Д゚)ウマー

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使ってみた感じ。

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.vim便利す。

*ack.txt*   Plugin that integrates ack with Vim
 
==============================================================================
Author:  Antoine Imbert <[email protected]>         *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


About

@glidenote

このブログは個人的なものです。ここで述べられている内容は私の個人的な意見に基づくものであり、私の雇用者に一切の関係はありません