LoginSignup
33
47

More than 3 years have passed since last update.

MSYS2 +α - 構築手順・備忘録

Last updated at Posted at 2018-07-29

MSYS2のインストール、設定方法について書かれた様々なサイトを参考にしながら
環境構築を試してきましたが、pacmanのバージョンの違いなどによって、意外とつまずく点もあったため、
自分用の構築手順をまとめて記載していこうと思います。
MSYS2のインストールやパッケージ更新以外は各設定の備忘録になります。

1.MSYS2のインストール
2.pacmanによるパッケージ更新&追加
3.minttyの設定
4.Vimの設定
5.aliasの設定
6.functionsの設定
7.tmuxの設定
8.コンテキストメニューへの追加

1.MSYS2のインストール

まずは↓からインストーラをダウンロードしインストールします。
MSYS2 installer
"x86_64" と書かれているほうは64bitです。
※ここでは64bitをインストールして設定していきます。

2.pacmanによるパッケージ更新&追加

まずはパッケージ管理の「pacman」のバージョンを確認します。

$ pacman -Sl | grep 'pacman '
msys pacman 5.0.1-1 [インストール済み]

更新方法等はpacmanのバージョンによって手順が異なるようですが、
この投稿では5.0.1-1または5.0.1.6403以降の場合で記載します。

2-1.更新

下記のコマンドで、
リポジトリとローカルパッケージデータベースとの同期およびアップグレード(ダウングレード含む)
を実行します。

$ pacman -Syuu

もし、下記の警告メッセージが表示された場合は
MSYS2を再起動し、再度更新のコマンドを実行します。
上記を繰り返し、更新するものがなくなれば警告メッセージは表示されなくなります。

警告: terminate MSYS2 without returning to shell and check for updates again
警告: for example close your terminal window instead of calling exit

2-2.追加

パッケージの更新が終われば、次は必要なパッケージの追加です。
パッケージのインストールは下記のコマンドです。

$ pacman -S [パッケージ名]

自分の環境には下記のパッケージをインストールしました。

$ pacman -S base-devel
$ pacman -S msys2-devel
$ pacman -S mingw-w64-i686-toolchain
$ pacman -S mingw-w64-x86_64-toolchain
$ pacman -S mingw-w64-x86_64-boost
$ pacman -S vim
$ pacman -S openssh
$ pacman -S sshpass
$ pacman -S git
$ pacman -S unzip
$ pacman -S rsync
$ pacman -S tmux

// 一括インストールする場合
$ pacman -S base-devel msys2-devel mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain mingw-w64-x86_64-boost vim openssh sshpass git unzip rsync tmux

3.minttyの設定

minttyはターミナルエミュレータです。
設定ファイルは「.minttyrc」になります。

.minttyrc
# mintty Settings
BoldAsFont=no
FontHeight=11

Columns=110
Rows=40
Locale=ja_JP
Charset=UTF-8
Term=xterm-256color

CopyOnSelect=yes
RightClickAction=paste
BackspaceSendsBS=yes

IMECursorColour=255,0,0

4.Vimの設定

Vimはテキストエディタです。
設定ファイルは「.vimrc」になります。

.vimrc
" Vim Settings

"カラースキーム
syntax on
colorscheme monokai
set t_Co=256

" setting
" 文字コードをUTF-8に設定
set fenc=utf-8
" バックアップファイルを作らない
set nobackup
" スワップファイルを作らない
set noswapfile
" 編集中のファイルが変更されたら自動で読みなおす
set autoread
" バッファが編集中でもその他のファイルを開けるように
set hidden
" 入力中のコマンドをステータスに表示する
set showcmd
" Backspaceによる削除を設定
set backspace=indent,eol,start

" 見た目系
" 行番号を表示
set number
" 行末の一文字先までカーソルを移動できるように
set virtualedit=onemore
" インデントはスマートインデント
" set smartindent
" 改行時に前の行のインデントを継続する
set autoindent
" 括弧入力時の対応する括弧を表示
set showmatch
" ステータスラインを常に表示
set laststatus=2
" 現在の行を強調表示
set cursorline
" コマンドラインの補完
set wildmode=list:longest
" 折り返し時に表示行単位での移動できるようにする
nnoremap j gj
nnoremap k gk
" 行頭、行末移動
nnoremap <S-h> 0
nnoremap <S-l> $
" ノーマルモード時にReturnキーで改行
nnoremap <CR> A<CR><ESC>
" nnoremap ; :
" nnoremap : ;


" Tab系
" 行頭以外のTab文字の表示幅(スペースいくつ分)
" set tabstop=4
" 行頭でのTab文字の表示幅
set shiftwidth=4
" 行末の1文字先までカーソルを移動できるように
set virtualedit=onemore


" 検索系
" 検索文字列入力時に順次対象文字列にヒットさせる
set incsearch
" 検索文字列が小文字の場合は大文字小文字を区別なく検索する
set ignorecase
" 検索文字列に大文字が含まれている場合は区別して検索する
set smartcase
" 検索語をハイライト表示
set hlsearch
" ESC連打でハイライト解除
nnoremap <Esc><Esc> :nohlsearch<CR><Esc>
" 検索時に何個マッチしたかを表示する
nnoremap <expr> / _(":%s/<Cursor>/&/gn")


" 保存時に行末の空白を除去する
" autocmd BufWritePre * :%s/\s\+$//ge
" 保存時にtabを2スペースに変換する
" autocmd BufWritePre * :%s/\t/    /ge

"hi IndentGuidesOdd  ctermbg=black
"hi IndentGuidesEven ctermbg=darkgrey

" Ctrl+nでディレクトリ構造を表示
" nnoremap <C-n> :NERDTreeToggle<Enter>


" http://inari.hatenablog.com/entry/2014/05/05/231307
""""""""""""""""""""""""""""""
" 全角スペースの表示
"""""""""""""""""""""""""""""
function! ZenkakuSpace()
  highlight ZenkakuSpace cterm=underline ctermfg=lightblue guibg=darkgray
endfunction

if has('syntax')
  augroup ZenkakuSpace
    autocmd!
    autocmd ColorScheme * call ZenkakuSpace()
    autocmd VimEnter,WinEnter,BufRead * let w:m1=matchadd('ZenkakuSpace', ' ')
  augroup END
  call ZenkakuSpace()
endif
""""""""""""""""""""""""""""""


" https://sites.google.com/site/fudist/Home/vim-nihongo-ban/-vimrc-sample
""""""""""""""""""""""""""""""
" 挿入モード時、ステータスラインの色を変更
""""""""""""""""""""""""""""""
let g:hi_insert = 'highlight StatusLine guifg=darkblue guibg=darkyellow gui=none ctermfg=white ctermbg=darkgray cterm=none'

if has('syntax')
  augroup InsertHook
    autocmd!
    autocmd InsertEnter * call s:StatusLine('Enter')
    autocmd InsertLeave * call s:StatusLine('Leave')
  augroup END
endif

let s:slhlcmd = ''
function! s:StatusLine(mode)
  if a:mode == 'Enter'
    silent! let s:slhlcmd = 'highlight ' . s:GetHighlight('StatusLine')
    silent exec g:hi_insert
  else
    highlight clear StatusLine
    silent exec s:slhlcmd
  endif
endfunction

function! s:GetHighlight(hi)
  redir => hl
  exec 'highlight '.a:hi
  redir END
  let hl = substitute(hl, '[\r\n]', '', 'g')
  let hl = substitute(hl, 'xxx', '', '')
  return hl
endfunction
"""""""""""""""""""""""""""""

5.aliasの設定

aliasはいわゆる「別名」のことで、
コマンドやオプションの組み合わせなどに別名を付けることで
コマンド入力の代わりに別名で実行することが可能になります。
設定ファイルは「.bash_aliases」になります。

.bash_aliases
# lloading message
echo 'load file: .bash_aliases'

alias ls='ls --color=auto --show-control-chars --time-style=long-iso -FH'
alias al='ls -al --color=auto --show-control-chars --time-style=long-iso -FH'
alias less='less -XFN'
alias vi='vim -u NONE --noplugin '
alias .alias='vim ~/.bash_aliases && source ~/.bash_aliases'

# ssh
alias ssh_sample1='sshpass -p hogehoge ssh -p 22 sample1@sample1.com'
alias ssh_sample2='sshpass -p hogehoge -P "Enter passphrase for key" ssh -o StrictHostKeyChecking=no -p 10022 sample2@sample2.com -i /c/sample2/key/id_rsa_sample2'

# 再起動
alias reboot='exec $SHELL -l'

alias grep='grep --color'

6.functionsの設定

関数を定義します。
設定ファイルは「.bash_function」になります。

.bash_function
# Functions
echo 'load file: .bash_function'

# Functions for MSYS2 bash

# File download by SCP command.
function download()
{
    source=$1
    dest=$2
    host=$3
    port=$4

    if [ $# -eq 3 ]; then
        scp ${host}:${source} ${dest}
    else
        scp -P ${port} ${host}:${source} ${dest}
    fi
}

# File upload by SCP command.
function upload()
{
    source=$1
    dest=$2
    host=$3
    port=$4

    if [ $# -eq 3 ]; then
        scp ${source} ${host}:${dest}
    else
        scp -P ${port} $source} ${host}:${dest}
    fi
}

# charset convert
function wincmd() {
    CMD=$1
    shift
    $CMD $* 2>&1 | iconv -f cp932 -t utf-8
}

# Windows Command
alias ipconfig='wincmd ipconfig'
alias java='wincmd java'
alias composer='wincmd composer'
alias php='wincmd php'

# winpty command
alias mysql='winpty mysql'
alias netstat='winpty netstat'
alias netsh='winpty netsh'

# pingのコマンド名混同防止対策で絶対パス指定
alias ping='wincmd /c/windows/system32/ping'

# package list
function plist()
{
    pacman -Sl | grep 'mingw64'
}

# package information
function pinfo()
{
    pacman -Sii "mingw-w64-x86_64-$1"
}

# package install
function pinst()
{
    pacman -S "mingw-w64-x86_64-$1"
}

# package uninstall
function puninst()
{
    pacman -Rs "mingw-w64-x86_64-$1"
}

# others
function snow()
{
    clear;while :;do echo $LINES $COLUMNS $(($RANDOM%$COLUMNS));sleep 0.1;done|gawk '{a[$3]=0;for(x in a) {o=a[x];a[x]=a[x]+1;printf "\033[%s;%sH ",o,x;printf "\033[%s;%sH*\033[0;0H",a[x],x;}}'
}

function matrix()
{
    echo -e "\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|gawk '{ letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
}

7.tmuxの設定

先ほどインストールしたパッケージの
tmux の設定を変更します。
設定ファイルは「.tmux.conf」です。

.tmux.conf
#+----------------------------------------------------------------------------------------+
#  セッションオプション
#+----------------------------------------------------------------------------------------+
#新しいウィンドウのベース番号
set-option -g base-index 1
#全てのベルを無視
set-option -g bell-action none
#各セッションで保持するバッファ数
#set-option -g buffer-limit 20
#256色端末を使用
set-option -g default-terminal "screen-256color"
#ウィンドウ履歴で保持される最大行数
set-option -g history-limit 5000
#Escapeキー入力時の待ち時間(0.5)をキャンセル
set-option -s escape-time 0

# vim colorscheme settings


#+----------------------------------------------------------------------------------------+
#  ステータスライン
#+----------------------------------------------------------------------------------------+
#status-left 及び status-right の非ASCII文字をUTF-8として扱う
#set-option -g status-utf8 on

#set-option -g status-left '#[fg=white,bg=blue][S:#S]#[default]'
#set-option -g status-right '#[fg=black,bg=cyan][CPU=#(getCpuUsage.sh) MEM=#(getMemUsage.sh)]#[fg=white,bg=blue][%Y/%m/%d(%a)%H:%M]#[default]'

set-option -g status-left '#[fg=black,bg=green][S:#S]#[default]'
set-option -g status-right '#[fg=black,bg=green][CPU=#(getCpuUsage.sh) MEM=#(getMemUsage.sh)]#[fg=black,bg=green][%Y/%m/%d(%a)%H:%M]#[default]'

set-option -g status-right-length 50
#set-option -g status-fg white
#set-option -g status-bg black
#set-window-option -g window-status-fg blue
#set-window-option -g window-status-bg black
#set-window-option -g window-status-current-fg white
#set-window-option -g window-status-current-bg blue

#+----------------------------------------------------------------------------------------+
#  ウィンドウオプション
#+----------------------------------------------------------------------------------------+
#ウィンドウを実行コマンド名で自動リネーム
set-window-option -g automatic-rename on
#スクロールモード、コピーモード、選択モードで vi のキーバインドを使う
set-window-option -g mode-keys vi
#ウィンドウで動作があるとステータスラインでハイライト
set-window-option -g monitor-activity on
#UTF-8 シーケンスが現れることに備える
#set-window-option -g utf-8 on

#+----------------------------------------------------------------------------------------+
#  キーバインド
#+----------------------------------------------------------------------------------------+
#プレフィックスキーを設定
unbind-key C-b
set-option -g prefix C-j
bind-key C-j send-prefix

# prefix + r で設定ファイルを再読み込み
unbind r
bind r source-file ~/.tmux.conf

#ペインを水平・垂直分割
bind-key s split-window -h
bind-key v split-window -v
#h,j,k,lでペインを切り替え
bind-key h select-pane -L
bind-key j select-pane -D
bind-key k select-pane -U
bind-key l select-pane -R

8.コンテキストメニューへの追加

おまけとして、コンテテキストメニューにMSYS2 で開くみたいなのを追加して
作業したいフォルダで直接 MSYS2 が起動できるようにします。

レジストリエディタでキーを登録していっても良いのですが、
regファイルを作成したので、それをインポートする方法を記載します。
※本記事を参考にされる場合、レジストリの編集は自己責任でお願いします。

msys2.reg
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\msys2]
@="MSYS2 で開く"
"Icon"="\"C:\\msys64\\msys2.ico\""

[HKEY_CLASSES_ROOT\Directory\shell\msys2\command]
@="\"C:\\msys64\\msys2_shell.cmd\" -where \"%V\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\msys2]
@="MSYS2 で開く"
"Icon"="\"C:\\msys64\\msys2.ico\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\msys2\command]
@="\"C:\\msys64\\msys2_shell.cmd\" -where \"%V\""

regファイルの記述で重要な点は
・先頭行にレジストリエディタのバージョンを指定
・キーごとに必ず空行を空ける。
の2点になります。

regファイルが作成できたら、レジストリエディタを開いて1
[ファイル]→[インポート]
でインポートすれば、キーが登録されます。

reg.png

登録した後は、任意のフォルダ等を右クリックすれば
コンテテキストメニューに 「MSYS2 で開く」 が表示され、
それをクリックすれば該当のパスで MSYS2 が起動するはずです。


  1. Win + R で 「ファイル名を指定して実行」 を開き、regedit と入力して Enter。など 

33
47
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
33
47