エンジニアリングとお金の話

都内で働くエンジニアの日記です。

【技術】会社の端末を理想の開発環境にしてみた

【SPONSORED LINK】

自宅ではmacを使用して開発を行っているが、会社ではwindows端末が支給され使用している。基本vimpythonが入っていれば開発は行えるが、自宅での開発と比べると使いづらく不満を感じていた。

ちなみに、自宅では以下の開発環境を使用している。

ターミナル:iterm2
OS    :macOS Sierra
シェル  :zsh
エディタ :vim8
言語   :python3.5

主な使い方としてはiterm2上でvimを立ち上げてpythonを書くといった感じである。また、途中でipythonやコマンドを実行したくなった時はターミナルを分割して実行している。

なるべく同じような操作感を会社の端末でも実現したいと思い、試行錯誤の結果以下の環境に辿りついた。

ターミナル:rLogin
OS    :CentOS7
シェル  :zsh
エディタ :vim8
言語   :python3.5

構築手順は以下の通り。

1.rLoginのインストール

http://nanno.dip.jp/softlib/man/rlogin/

iterm2と同じようにターミナルを分割する事ができる。デフォルトでのキー設定では、ctrl + 矢印キーでの分割となっているが自分はAlt + 矢印キーで画面分割、ctrl + 矢印キーで画面移動と設定している。キー設定の変更はキーボード表示⇒オプション設定⇒キーボードで変更可能。

2.VirturlBoxのインストール

https://www.virtualbox.org/

3.Vagrantのインストール

https://www.vagrantup.com/

4.CentOS7のインストール

HashCorpが提供しているCentOS7を使用。以下のコマンドを実行。

vagrant init centos/7; vagrant up --provider virtualbox

5.OSを立ち上げ、rLoginにて接続

vagrant up

7.zshをインストール

$ sudo yum install zsh  
$ sudo yum install zsh-completions  
$ sudo yum install zsh-syntax-highlighting  

8.ログインシェルをzshに変更

$ chsh -s /bin/zsh

6.pyenvをインストール

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv

7. pyenvを動作させる為、.zshrcに以下を記述

export PYENV_ROOT=$HOME/.pyenv  
export PATH=$PYENV_ROOT/bin:$PATH  
eval "$(pyenv init -)"  

記述後 source .zshrcを実行し環境変数に反映。

8.pythonインストール

$ pyenv install anaconda3-4.1.1

9.pythonの切り替え

$ pyenv global anaconda3-4.1.1

10.仮想環境の構築

環境を分ける事が出来るようにcondaにて仮想環境を構築。

conda create -n py35 python=3.5 anaconda

11.仮想環境のpythonに切り替え

$ source /home/vagrant/.pyenv/versions/anaconda3-4.1.1/envs/py35/bin/activate py35

12.pipにてpowerlineをインストール

$ pip install --user powerline-status

13.powerlineを起動させる為、.zshrcに以下を記述

if [ -d ~/.local/bin ]; then  
  export PATH="$PATH":~/.local/bin  
  powerline-daemon -q  
  . ~/.local/lib/python3.5/site-packages/powerline/bindings/zsh/powerline.zsh  
fi  

14.powerline用のフォントをダウンロードしrLoginにて設定

https://github.com/powerline/fontsよりフォントをダウンロード。
※自分はRobotMonoを使用している。

15.polwelineの設定を変更

デフォルトの設定では、マルチバイト文字の影響にてステータスラインが改行されて見づらいので以下の通り修正。

/home/vagrant/.local/lib/python3.5/site-packages/powerline/config_files/themes/powerline.json

・4行目の"hard"の値を"$ "に変更
・5行目の"soft"の値を"> "に変更
・8行目の"hard"の値を""に変更
・9行目の"soft"の値を""に変更

/home/vagrant/.local/lib/python3.5/site-packages/powerline/config_files/themes/shell/main.json

・10行目の"dir_limit_depth"の値を"1"に変更

/home/vagrant/.local/lib/python3.5/site-packages/powerline/config_files/themes/shell/default.json

・"right"の設定値を削除

16.vimのインストール

ソースからインストール。

$ git clone https://github.com/vim/vim /usr/local/src  

$ sudo ./configure -n --enable-fail-if-missing\  
--with-features=huge \  
--disable-selinux \  
--enable-luainterp \  
--enable-perlinterp \  
--enable-python3interp \   
--enable-rubyinterp \   
--enable-cscope \  
--enable-fontset \  
--enable-multibyte \  
--with-python3-config-dir=/home/vagrant/.pyenv/versions/anaconda3-4.1.1/lib/python3.5/config-3.5m \  
--with-ruby-command=/usr/bin/ruby \  
vi_cv_path_python3=/home/vagrant/.pyenv/versions/anaconda3-4.1.1/bin/python3.5  
 
$ make  
 
$ sudo make install

※足りないモジュールがあったら適宜yumにてインストール

19.vimプラグインのインストール

Dein.vimのインストール。

$ curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh  
$ sh ./installer.sh ~/.vim/dein  

20.dotファイルの設定

.vimrc

""""""""""""""""""""""""基本設定 開始""""""""""""""""""""""""
"文字コード設定
set encoding=utf-8
set fileencodings=utf-8,cp932,euc-jp

"改行コード設定
set fileformats=unix,dos,mac

"表示設定
set number          "行数表示
set nowrap          "折り返し無し
set ruler           "カーソル位置を表示
set textwidth=0     "自動的に改行が入るのを無効化
set showcmd         "入力中のコマンドを表示
set title           "編集中のファイル名を表示
set showmatch       "対応するカッコを強調表示
set matchtime=1     "対応するカッコを強調表示時の時間を設定
set laststatus=2    "ステータス行を常に表示
set showtabline=2   "タブのラベルを表示
set showmode        "現在のモードを表示

"バックスペースキー使用設定
set backspace=indent,eol,start

"バックアップファイル設定
set nowritebackup
set nobackup
set noswapfile
set noundofile

"タブ設定
set expandtab      "タブを半角空白として設定
set tabstop=8      "タブを半角スペース8文字として表示(pythonのインタプリタはタブ文字を8文字として扱う為)
set softtabstop=4  "タブ入力時半角スペース4文字として表示
set shiftwidth=4   "インデント時に半角スペース4文字として表示

"背景色設定
colorscheme torte
set t_Co=256

"補完設定
set completeopt=menu

"カレントディレクトリ設定(自動的に開いたファイルのディレクトリに移動)
if exists('+autochdir')
  set autochdir
endif

"クリップボード設定
set clipboard=unnamed

"検索関連
set incsearch                   "インクリメンタルサーチ
set hlsearch                    "検索ハイライト
set wrapscan                    "行末まで検索した先頭に戻る
set smartcase                   "大文字と小文字を無視して検索(小文字時)
set ignorecase                  "大文字と小文字を無視して検索
noremap <Leader>nh :nohlsearch<CR> "検索ハイライト削除

"矩形選択設定
set virtualedit=block

"勝手にコメントアウトしない様に対応
augroup auto_comment_off
    autocmd!
    autocmd BufEnter * setlocal formatoptions-=r
    autocmd BufEnter * setlocal formatoptions-=o
augroup END

" make, grep などのコマンド後に自動的にQuickFixを開く
autocmd  QuickfixCmdPost make,grep,grepadd,vimgrep copen

" QuickFixおよびHelpでは q でバッファを閉じる
autocmd  FileType help,qf nnoremap <buffer> q <C-w>c

"コード編集設定
filetype plugin indent on  "ファイル形式別プラグイン有効化
syntax enable              "シンタックスハイライト
""""""""""""""""""""""""基本設定 終了""""""""""""""""""""""""


""""""""""""""""""""""""言語個別設定 開始""""""""""""""""""""""""
"python用 インデント設定
autocmd FileType python setl autoindent  "オートインデント有効化
autocmd FileType python setl smartindent "スマートインデント有効化
"該当するワード時に自動的にインデント
autocmd FileType python setl cinwords=if,elif,else,for,while,try,except,finally,def,class 
""""""""""""""""""""""""言語個別設定 終了""""""""""""""""""""""""


""""""""""""""""""""""""プラグイン導入設定 開始""""""""""""""""""""""""
if &compatible
  set nocompatible
endif

set runtimepath^=~/.vim/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand('~/.vim/dein'))

call dein#add('Shougo/dein.vim')
call dein#add('vim-jp/vimdoc-ja')
call dein#add('thinca/vim-quickrun')
call dein#add('Shougo/unite.vim')
call dein#add('Shougo/neomru.vim')
call dein#add('Shougo/unite-outline')
call dein#add('Shougo/neoyank.vim')
call dein#add('Shougo/neosnippet')
call dein#add('Shougo/neosnippet-snippets')
call dein#add('kana/vim-operator-user')
call dein#add('kana/vim-textobj-user')
call dein#add('kana/vim-operator-replace')
call dein#add('rhysd/vim-operator-surround')
call dein#add('tpope/vim-surround')
call dein#add('nathanaelkane/vim-indent-guides')
call dein#add('Shougo/neocomplete.vim')
call dein#add('Shougo/vimproc', {'build':'make'})
call dein#add('davidhalter/jedi-vim')
call dein#add('powerline/powerline', {'rtp': 'powerline/bindings/vim/'})
call dein#add('flazz/vim-colorschemes')
call dein#add('junegunn/vim-easy-align')
call dein#add('bronson/vim-trailing-whitespace')
call dein#add('simeji/winresizer')

call dein#end()

filetype plugin indent on

" If you want to install not installed plugins on startup.
if dein#check_install()
  call dein#install()
endif
""""""""""""""""""""""""プラグイン導入設定 終了""""""""""""""""""""""""


""""""""""""""""""""""""プラグイン個別設定 開始""""""""""""""""""""""""

"---------------------- Unite ----------------------
" バッファ一覧
nnoremap <C-u>b :<C-u>Unite buffer<CR>

" ファイル一覧
nnoremap <C-u>f :<C-u>UniteWithBufferDir -buffer-name=files file<CR>

" レジスタ一覧
nnoremap <C-u>r :<C-u>Unite -buffer-name=register register<CR>

"ヤンク
nnoremap <C-u>y :<C-u>Unite history/yank<CR>

" 最近使用したファイル一覧
nnoremap <C-u>m :<C-u>Unite file_mru<CR>

" 常用セット
nnoremap <C-u>u :<C-u>Unite buffer file_mru<CR>

" 全部乗せ
"nnoremap <C-u>a :<C-u>UniteWithBufferDir -buffer-name=files buffer file_mru bookmark file<CR>
nnoremap <C-u>a :<C-u>UniteWithBufferDir -buffer-name=files buffer file_mru  file<CR>
" Unit outline(ソースコード解析)
nnoremap <C-u>o :<C-u>Unite outline<CR>


" ウィンドウを分割して開く
autocmd FileType unite nnoremap <silent> <buffer> <expr> <C-j> unite#do_action('split')
autocmd FileType unite inoremap <silent> <buffer> <expr> <C-j> unite#do_action('split')

" ウィンドウを縦に分割して開く
autocmd FileType unite nnoremap <silent> <buffer> <expr> <C-l> unite#do_action('vsplit')
autocmd FileType unite inoremap <silent> <buffer> <expr> <C-l> unite#do_action('vsplit')
"---------------------- Unite ----------------------


"---------------------- quickrun ----------------------
let g:quickrun_config={}
let g:quickrun_config["_"] = {'into':1}
"---------------------- quickrun ----------------------


"---------------------- indent-guides ----------------------
let g:indent_guides_enable_on_vim_startup=1
let g:indent_guides_color_change_percent=30
let g:indent_guides_guide_size=1
if has('win32') || has('win64')
    let g:indent_guides_auto_colors = 1
else
    let g:indent_guides_auto_colors = 0
    autocmd VimEnter,Colorscheme * :hi IndentGuidesOdd   ctermbg=darkblue
    autocmd VimEnter,Colorscheme * :hi IndentGuidesEven  ctermbg=darkgreen
endif
"---------------------- indent-guides ----------------------


"---------------------- vim-operator-replace ----------------------
nmap R <Plug>(operator-replace)
"---------------------- vim-operator-replace ----------------------


"---------------------- vim-operator-surround ----------------------
nmap <silent>sa <Plug>(operator-surround-append)
nmap <silent>sd <Plug>(operator-surround-delete)
nmap <silent>sr <Plug>(operator-surround-replace)
"---------------------- vim-operator-surround ----------------------


"----------------------  vim-easy-align ----------------------
vmap <Enter> <Plug>(EasyAlign)
"----------------------  vim-easy-align ----------------------


"----------------------  vim-trailing-whitespace ----------------------
if dein#tap('vim-trailing-whitespace')
    " uniteでスペースが表示されるので、設定でOFFにする
    let g:extra_whitespace_ignored_filetypes = ['unite']
endif
"----------------------  vim-trailing-whitespace  ----------------------


"---------------------- neocomplete.vim ----------------------
" 起動時に有効化
let g:neocomplete#enable_at_startup = 1
"ポップアップルメニューで表示される候補の数
let g:neocomplete#max_list = 20
"曖昧検索を行わない
let g:neocomplete#enable_fuzzy_completion=0
"---------------------- neocomplete.vim ----------------------


"---------------------- jedi-vim ----------------------
""jedi初期設定を行う。omnifuncにjedi#completionsを設定
let g:jedi#auto_initialization = 1

"jedi独自のプレビュ設定を行わない
let g:jedi#auto_vim_configuration = 0

"変数の宣言場所へジャンプ
let g:jedi#goto_assignments_command = "<C-j>a"

"クラス、関数定義にジャンプ
let g:jedi#goto_definitions_command = "<C-j>f"

" Pydocを表示(Ctrl + k)
let g:jedi#documentation_command = "<C-j>h"

"リネーム
let g:jedi#rename_command = "<C-j>r"

"変数表示
let g:jedi#usages_command = "<C-j>l"

" 補完の最初の項目が選択された状態だと使いにくいためオフにする
let g:jedi#popup_select_first = 0

"ポップアップヒントを表示させない
let g:jedi#show_call_signatures = 0

"ポップアップを表示しない
autocmd FileType python setlocal completeopt-=preview

"neocompleteと連携用の設定
if ! empty(dein#get("neocomplete.vim"))
    autocmd FileType python setlocal omnifunc=jedi#completions

    let g:jedi#completions_enabled = 0
    let g:jedi#auto_vim_configuration = 0

    if !exists('g:neocomplete#force_omni_input_patterns')
            let g:neocomplete#force_omni_input_patterns = {}
    endif

    let g:neocomplete#force_omni_input_patterns.python = '\h\w*\|[^. \t]\.\w*'
endif
"---------------------- jedi-vim ----------------------

.zshrc

########################################
# 文字コード
export LANG=ja_JP.UTF-8

# 色を使用出来るようにする
autoload -Uz colors
colors

# emacs 風キーバインドにする
bindkey -e

# ヒストリの設定
HISTFILE=~/.zsh_history
HISTSIZE=1000000
SAVEHIST=1000000

# 単語の区切り文字を指定する
autoload -Uz select-word-style
select-word-style default
# ここで指定した文字は単語区切りとみなされる
# / も区切りと扱うので、^W でディレクトリ1つ分を削除できる
zstyle ':zle:*' word-chars " /=;@:{},|"
zstyle ':zle:*' word-style unspecified

########################################

# 補完機能を有効にする
autoload -Uz compinit
compinit
zstyle ':completion:*:default' menu select=1

# 補完候補をTABで移動
setopt auto_menu

# 補完候補を詰めて表示
setopt list_packed

#  補完候補を一覧表示
setopt auto_list

# 補完で小文字でも大文字にマッチさせる
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# ../ の後は今いるディレクトリを補完しない
zstyle ':completion:*' ignore-parents parent pwd ..

# sudo の後ろでコマンド名を補完する
zstyle ':completion:*:sudo:*' command-path /usr/local/sbin /usr/local/bin \
                   /usr/sbin /usr/bin /sbin /bin /usr/X11R6/bin

# ps コマンドのプロセス名補完
zstyle ':completion:*:processes' command 'ps x -o pid,s,args'

########################################

# vcs_info
autoload -Uz vcs_info
autoload -Uz add-zsh-hook

zstyle ':vcs_info:*' formats '%F{green}(%s)-[%b]%f'
zstyle ':vcs_info:*' actionformats '%F{red}(%s)-[%b|%a]%f'

function _update_vcs_info_msg() {
    LANG=en_US.UTF-8 vcs_info
    RPROMPT="${vcs_info_msg_0_}"
}
add-zsh-hook precmd _update_vcs_info_msg

########################################

# オプション
# 日本語ファイル名を表示可能にする
setopt print_eight_bit

# beep を無効にする
setopt no_beep

# フローコントロールを無効にする
setopt no_flow_control

# Ctrl+Dでzshを終了しない
setopt ignore_eof

# '#' 以降をコメントとして扱う
setopt interactive_comments

# cd したら自動的にpushdする
setopt auto_pushd

# 重複したディレクトリを追加しない
setopt pushd_ignore_dups

# 同時に起動したzshの間でヒストリを共有する
setopt share_history

# 同じコマンドをヒストリに残さない
setopt hist_ignore_all_dups

# スペースから始まるコマンド行はヒストリに残さない
setopt hist_ignore_space

# ヒストリに保存するときに余分なスペースを削除する
setopt hist_reduce_blanks

# 高機能なワイルドカード展開を使用する
setopt extended_glob

# コマンドミスを修正
setopt correct

# 最後のスラッシュを自動的に削除しない
setopt noautoremoveslash

########################################

#エイリアス
alias ll='ls -l'
alias la='ls -a'

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

alias mkdir='mkdir -p'

alias vi='vim'

# sudo の後のコマンドでエイリアスを有効にする
alias sudo='sudo '

########################################

##pyenv config
export PYENV_ROOT="${HOME}/.pyenv"
export PATH="${PYENV_ROOT}/bin:$PATH"
eval "$(pyenv init -)"

#python 3
pyenv global anaconda3-4.1.1
source /home/vagrant/.pyenv/versions/anaconda3-4.1.1/envs/py35/bin/activate py35

########################################

#power line
if [ -d ~/.local/bin ]; then
  export PATH="$PATH":~/.local/bin
  powerline-daemon -q
  . ~/.local/lib/python3.5/site-packages/powerline/bindings/zsh/powerline.zsh
fi

########################################

#zsh-completions
if [ -d /usr/local/share/zsh-completions ]; then
  fpath=(/usr/local/share/zsh-completions $fpath)
fi
autoload -U compinit
compinit -C

########################################

#zsh-syntax-highlighting
if [ -f /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]; then
      source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi

########################################

まとめ

windows上でも自宅とほぼ同じような感覚で作業出来てすごく快適になった。 今年はこの環境で色々な開発を行って行きたい。

※参考 MacOS10.11環境構築: AnacondaとDein.vimでPowerline - Qiita