LoginSignup
1
2

More than 5 years have passed since last update.

MacのvimでEditorConfigを効かせるハナシ

Last updated at Posted at 2018-10-21

この文書は

MacOS High Sierra環境で、brew install vim editorconfigした上で、.editorconfigの使いかたが分からなかったので試してみた、という話。

分からないポイント

  • EditorConfig-Vimのドキュメントに書いてあるといえば書いてあるけれど、.vimrcの書き方とか分からない
  • Vimのプラグインの仕組みを知ろうとすると、VundleとかNeoBundleとかDeinとかいろんなやり方があって、血で血を洗う抗争をしているらしいことしか分からない

単に、vimの機能拡張をする使い方に詳しくないというだけの話かもしれない。

やってみた

EditorConfig-Vimのドキュメントに書いてある方法

最初の方法

  • Download the archive and extract it into your Vim runtime directory (~/.vim on UNIX/Linux and $VIM_INSTALLATION_FOLDER\vimfiles on windows). You should have 3 sub-directories in this runtime directory now: "autoload", "doc" and "plugin".

おっしゃるように、git clone https://github.com/editorconfig/editorconfig-vim.git してから、.vimに、autoloaddocpluginとをcp -pRしてコピーする。

この状態で、.vimrcがない状態で、とりあえず、indent_style=tabとかindent_style=spaceとかを適当に設定を変えてみると、その通りに動作する。これなら、問題ないんだけれど、.vimrcが空の状態では、いろいろ都合が悪い。

とりあえず、以下の設定だけはしてみた。

syntax on
filetype plugin on
filetype indent on

pathogenを使う方法

最初の方法でイケたので、これ以上何もする必要はないんだけれど、pathogenというのも試してみることにする。

先ほど作成した.vimは潔く消してしまうゼ。ワイルドだろう?(ネタが古い)

まずはpathogenをインストールする。

mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

次に、さっき書いた.vimrcを編集する。

"pathogen
execute pathogen#infect()

syntax on
filetype plugin on
filetype indent on

そして、EditorConfigをインストールする。

cd ~/.vim/bundle/ && \
git clone https://github.com/editorconfig/editorconfig-vim.git

この状態で、indent_style=spaceな上で、indent_size=3みたいな、あんまりやらなさそうな設定でpythonのコードを書くと、ちゃんとソレっぽく書ける。

for n in range(0, 10):
   print(n)

Vundleを使う方法

  • Use Vundle by adding to your .vimrc Vundle plugins section:

Plugin 'editorconfig/editorconfig-vim'

まず、Vundleを使えるようにする。

mkdir -p ~/.vim/bundle && \
cd ~/.vim/bundle && \
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

次に、.vimrcを編集する。

set nocompatible
filetype off

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'editorconfig/editorconfig-vim'
call vundle#end()

syntax on
filetype plugin on
filetype indent on

vimを起動して、:PluginInstallしたら、ちゃんとEditorConfigの設定が効くようになった。

EditorConfig-Vimのドキュメントに書いてない方法

NeoBundleを使う方法

NeoBundleの配布元には、以下のように記載されているけれど、気にしない。

Note: Active developement on NeoBundle has stopped. The only future changes will be bug fixes.

Please see Dein.vim -- A faster, well-tested plugin manager for Vim and Neovim. It can do everything NeoBundle does, including asynchronous installs.

ドキュメントどおり、directoryを作って、そこにNeoBundleをインストールする。

mkdir ~/.vim/bundle && \
git clone https://github.com/Shougo/neobundle.vim ~/.vim/bundle/neobundle.vim

次に、.vimrcを編集する。

if &compatible
   set nocompatible
endif

set runtimepath+=~/.vim/bundle/neobundle.vim/

call neobundle#begin(expand('~/.vim/bundle/'))
NeoBundleFetch 'Shougo/neobundle.vim'
NeoBundle 'editorconfig/editorconfig-vim.git'
call neobundle#end()

filetype plugin on
filetype indent on
syntax on

NeoBundle 'editorconfig/editorconfig-vim.git'と書いておきさえすれば、NeoBundleInstall実行時に、実体がダウンロードされ使えるように設定されるようだ。

ということで、vimを起動して、:NeoBundleInstallしたら、使えるようになった。

Deinを使う方法

もうNeoBundleの時代ではなく、Deinの時代らしいので、ドキュメントに従い、インストールする。

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

ホームディレクトリの.cacheって、一時的なcacheとして使う場所というイメージがあるので、ここにインストールするという行為にはちょっと抵抗があるけれど、こうするのがオススメだというのであれば、仕方ない。

ちなみに、このinstall.shは、単にgit cloneして、.vimrcの書き方を表示してくれるものである。

これに従って、.vimrcを作成する。

if &compatible
   set nocompatible
endif

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

if dein#load_state('~/.cache/dein')
  call dein#begin('~/.cache/dein')
  call dein#add('~/.cache/dein/repos/github.com/Shougo/dein.vim')

  call dein#add('editorconfig/editorconfig-vim.git')
  call dein#end()
  call dein#save_state()
endif

filetype plugin indent on
syntax enable

次に、vimを起動して、:call dein#install()する。


本日の結論

以上のように、MacOS High Sierraでは、brew install vim editorconfigさえしておけば、いずれの方法でもvimでもEditorConfigを使えるようになることが分かった。

個人的には、GNU EmacsVisual Studio Codeを使うことが多いのだけれど、Terminal上で、数行ちょっとばかり手直ししたい時なんかにはvimの方が起動が速いし、そんなときにEditorConfigの設定が効くというのはありがたい。

本日の疑問

これをWindowsで、実現する方法が分からない。っていうか、Windowsのcmd.exeでもPowerShellでも、Git Bash上でも、Cygwinのminttyのzsh上でも使えるvimが存在するのかどうかがよく分からない。

1
2
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
1
2