LoginSignup
32
31

More than 5 years have passed since last update.

Vagrant with Sublime Text on Mac or Windows

Last updated at Posted at 2013-01-30

===========================================

Sublime Textで嬉しいのはMacやWindowsで利用できることです。
両方のOSで使うなら操作を合わせた方がなにかと便利です。

まずはMacとWindowsのキーバインドをできるだけ合わせます。
(KeyRemap4MacBookChange Keyなどを利用します。)

次にGitやPython,Ruby,Node.jsを利用するので、WindowsでUNIXシェルを使えるようにします。
(CygwinMSYSを利用します。それにしてもMacのターミナルは圧倒的に使いやすいです。)

さらにMySQL,Redis,MongoDB,RabbitMQなどミドルウェアを利用するのにWindowsでもよいのですが
非公式だったり最新版がない場合があるのでパッケージの管理が簡単なLinuxにミドルウェアを任せます。
(SSHで接続できるLinuxならグローバルでもローカルでもなんだっていいです。)

今回はVagrantとSublime Textを利用してMacとWindowsの環境を作ります。
大筋はMacとWindowsで利用できるVagrantVirtualBoxを操作して
ホストOSのSublime TextとゲストOSのUbuntuでファイルを連携します。

XcodeやVisual Studioのエディタ・ドリブンが好きなので、OSを意識することなく
エディタを中心に作業がしたいです。

Mac

OS XはUNIXベースなので簡単に環境が作れます。
(仮想イメージのダウンロード時間を除けば作業時間は10分程度です。)

環境

  • Mac OS X Lion
  • VirtualBox 4.2.6
  • Ruby 1.9.3-p374
  • Vagrant 1.0.6

Vagrant

VirtualBoxのDownloadsからfor OS Xをインストールします。
GemでVagrantをインストールして、仮想マシンのイメージを追加します。

$ gem install vagrant
$ vagrant box add precise64 http://files.vagrantup.com/precise64.box
[vagrant] Downloading with Vagrant::Downloaders::HTTP...
[vagrant] Downloading box: http://files.vagrantup.com/precise64.box
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

precise64はUbuntu12.04(64bit版)の仮想マシンのイメージです。

Virtual Machine

仮想マシンのディレクトリを作り、vagrant initコマンドに仮想マシンのイメージを指定します。
vagrant upコマンドで仮想マシンを起動して、vagrant sshコマンドでログインします。
Vagrantfileを編集すればchefなどの素敵な仕組みが利用できます。)

$ mkdir ubuntu12.04
$ cd ubuntu12.04
$ vagrant init precise64
$ vagrant up
$ vagrant ssh
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-23-generic x86_64)
vagrant@precise64:~$ mkdir notes

ホストOSに連携するディレクトリをゲストOSにあらかじめ作成します。
(ゲストOSはvagrant@precise64:~$と記します。)

Sublime Text

仮想マシンとファイルを連携するのにSublime SFTPを利用します。
Sublime SFTPは有料($16)のプラグインです。

$ mkdir notes
$ touch notes/foo
$ subl notes/

ちなみにsublコマンドはSublime TextのCLIです。

sftp-config.json

Sublime TextのサイドバーのSFTP/FTP -> Map to Remotesftp-config.jsonファイルを出力します。
host,user,port,ssh_key_file,remote_pathの値をVagrantと連携するように編集します。

{
    "type": "sftp",

    "save_before_upload": true,
    "upload_on_save": true,
    "sync_down_on_open": false,
    "sync_skip_deletes": false,
    "confirm_downloads": false,
    "confirm_sync": true,
    "confirm_overwrite_newer": false,

    "host": "127.0.0.1",
    "user": "vagrant",
    "port": "2222",
    "ssh_key_file": "~/.vagrant.d/insecure_private_key",

    "remote_path": "/home/vagrant/notes",
    "ignore_regexes": [
        "\\.sublime-(project|workspace)", "sftp-config(-alt\\d?)?\\.json",
        "sftp-settings\\.json", "/venv/", "\\.svn", "\\.hg", "\\.git",
        "\\.bzr", "_darcs", "CVS", "\\.DS_Store", "Thumbs\\.db", "desktop\\.ini"
    ],

    "connect_timeout": 30,
 }

Sync

ファイルを同期は特に意識しなくてもSublime Textで編集を保存するだけでゲストOSに自動で反映されます。
upload_on_savetrueが保存で同期する設定です。)

Sublime Textでfooファイルにberと入力して保存すると、ゲストOSの指定したディレクトリにfooファイルが作成されます。

vagrant@precise64:~$ cat notes/foo 
ber

VirtualBoxの共有フォルダーをマウントする機能もあるのですが
ファイルディスクリプタなど使い難かったのでSFTPにしました。


Windows

WindowsはUNIX,Linuxとは違い独特な所があるので合わせるのに工夫が必要です。
(仮想イメージのダウンロード時間を除けば作業時間は20分程度です。)

環境

  • Windows 7
  • VirtualBox 4.2.6
  • Ruby 1.9.3-p374
  • Vagrant 1.0.6

Mintty

ターミナルはMacと同じ様に背景の透過ができるMinttyを使います。
UnixシェルはCygwinではなくMSYSを使います。
MinGWDownloadsからmingw-get-instをインストールします。
MinGWのシェルでmsys-minttyをインストールします。

$ mingw-get update
$ mingw-get upgrade
$ mingw-get install msys-mintty

mintty.exeのショートカットを作成してリンク先(T)に/bin/bash --loginを追記します。
(mintty.exe\MinGW\msys\1.0\bin\にインストールされます。)

mintty設定

minttyを起動してオプションを設定します。

なるべくMacと同じフォントを使いましょう!
例ではRictyのフォントを設定しています。

Looks:Transparency = High
Text:Font = Ricty,14-point
Text:Locale = C
Text:Character set = UTF-8

Vagrant

VirtualBoxのDownloadsからfor Windowsをインストールします。
RubyはRuby for Windowsで安定版をインストールします。

$ gem install vagrant
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box

precise32はUbuntu12.04(32bit版)の仮想マシンのイメージです。

Virtual Machine

Macと同じ様にvagrant upコマンドで仮想マシンを起動して、vagrant sshコマンドをすると
vagrant sshコマンドはWindowsでは使えないと悲しいメッセージが表示されます。

$ mkdir ubuntu12.04
$ cd ubuntu12.04
$ vagrant init precise32
$ vagrant up
$ vagrant ssh
`vagrant ssh` isn't available on the Windows platform.

ssh

sshコマンドは使えるのでsshでログインはできるので後は接続情報があれば
と思ったらvagrant ssh-configコマンドがありました。(さすがっす!)

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile ...

alias

sshのオプションにファイルを指定するエイリアスを作成します。
vagrant-sshコマンドになりましたが、これでログインできます。

$ vagrant ssh-config > .ssh-config
$ echo 'alias vagrant-ssh="ssh default -F .ssh-config"' >> ~/.bash_profile
$ source ~/.bash_profile
$ vagrant-ssh
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-23-generic-pae i686)
vagrant@precise34:~$ mkdir notes

Sublime Text

後はSublime SFTPでゲストOSと連携と思ったらWindowsの秘密鍵はPuTTYとFAQに記載されています。

Vagrantの秘密鍵をPuTTY Key Generatorでコンバートします。
(Vagrantの秘密鍵を探すのが面倒ならCドライブの直下にコピーすると便利です。)

$ cat ~/.vagrant.d/insecure_private_key > /c/insecure_private_key

sftp-config.json

Macとはssh_key_fileの値だけが違います。

{
    "type": "sftp",

    "save_before_upload": true,
    "upload_on_save": true,
    "sync_down_on_open": false,
    "sync_skip_deletes": false,
    "confirm_downloads": false,
    "confirm_sync": true,
    "confirm_overwrite_newer": false,

    "host": "127.0.0.1",
    "user": "vagrant",
    "port": "2222",
    "ssh_key_file": "C:\\Users\\user\\.ssh\\vagrant.ppk",

    "remote_path": "/home/vagrant/notes",
    "ignore_regexes": [
        "\\.sublime-(project|workspace)", "sftp-config(-alt\\d?)?\\.json",
        "sftp-settings\\.json", "/venv/", "\\.svn", "\\.hg", "\\.git",
        "\\.bzr", "_darcs", "CVS", "\\.DS_Store", "Thumbs\\.db", "desktop\\.ini"
    ],

    "connect_timeout": 30,
 }

Sync

これでMacと同じ様にSublime Textでファイルを編集して保存すると、ゲストOSの指定したディレクトリにファイルが同期されます。

vagrant@precise32:~$ cat notes/foo 
ber

Browser Refresh for Sublime Text 2など自動で再起動してくれる
アプリケーションを組み合わせるとさらに作業が楽になります。

Tips

WindowsでもmsysgitをインストールするとSublime Text 2 plugin: gitでSublime Textから
ブランチを切替えたりcommitpushが使えるので便利です。

32
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
31