LoginSignup
10
5

More than 5 years have passed since last update.

vimrcのなんとか

Last updated at Posted at 2018-07-11

Summary

vimrc を書くのに必要最小限な知識のまとめ

Thanks

Vim-JP の掲示板のみなさま

Index

chapter title discription
01 minimal.vimrc 必要最小限のvimrcを作る
02 syntax 既存のカラースキームをちょっとだけ変更する
03 syntax 自作のカラースキームを作成する
04 map and noremap 自分好みのショートカットキーにする
05 options vimの設定を自分好みにする
06 user command 自作のコマンドを作成する
07 auto command いろいろなきっかけでコマンドを実行させる
08 Folding vimrcを管理しやすくするためにFoldingという機能をつかう
09 template vimrcのベストプラクティス
10 Vim script vimrcを作成するのに必要なVim scriptの最低限の知識
11 Reference vimrc作成に関してのいろいろな情報

01 minimal.vimrc

必要最小限の設定ファイルを書く
名前は minimal.vimrc

まずはここからスタート

# 作業用フォルダ foo を作る
$ mkdir foo/
# 作業用フォルダ foo に移動
$ cd foo/
# 設定ファイル minimal.vimrc を作成する
$ echo -e '"minimal.vimrc\n\nsyntax enable\nfiletype plugin indent on' > minimal.vimrc

vimrc

"minimal.vimrc
syntax enable
filetype plugin indent on

設定ファイル minimal.vimrc だけを使って vim をたちあげ

$ vim -N -u minimal.vimrc

02 syntax ( vimrc )

foofoofoo という文字を紫色にする

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" foofoofoo という文字を紫色にする
" vimrc で色付け
" --------------------------------------------------------
" create autocmd group
augroup foo-vimrc
 autocmd!
augroup END
syntax enable
filetype plugin indent on
" --------------------------------------------------------
-" local highlight group
+" define highlight group
" --------------------------------------------------------
" ハイライトグループ Foo を作成する
autocmd foo-vimrc ColorScheme * highlight Foo ctermfg=103 guifg=#8787af
" --------------------------------------------------------
" set color scheme
" --------------------------------------------------------
" colorscheme ron をセットする
colorscheme ron
" --------------------------------------------------------
-" local pattern to highlight group
+" add pattern to highlight group
" --------------------------------------------------------
" ハイライトグループ Foo に パターン を追加する
autocmd foo-vimrc FileType foo call matchadd( 'Foo', 'foofoofoo' )
" 拡張子が foo のファイルを filetype foo にする
autocmd foo-vimrc BufNewFile,BufRead *.foo setfiletype foo

foofoofooを入力してみる

$ vim -N -u minimal.vimrc foo.foo

Screen Shot 2018-07-11 at 11.22.07.png

03 syntax ( syntax file )

foofoofoo という文字を紫色にする

syntax フォルダ、syntax ファイルを作成する

# syntax フォルダをつくる
$ mkdir -p rc/syntax/
# syntax file を作成
$ vim rc/syntax/foo.vim
 syntax match Foo "foofoofoo"
 highlight Foo ctermfg=103 guifg=#8787af

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" foofoofoo という文字を紫色にする
" syntax file で色付け
" --------------------------------------------------------
" create autocmd group
augroup foo-vimrc
 autocmd!
augroup END
" syntax フォルダを path に追加する
set runtimepath+=./rc
syntax enable
filetype plugin indent on
" colorscheme ron をセットする
colorscheme ron
" 拡張子が foo のファイルを filetype foo にする
autocmd foo-vimrc BufNewFile,BufRead *.foo setfiletype foo

foofoofooを入力してみる

$ vim -N -u minimal.vimrc foo.foo

Screen Shot 2018-07-11 at 11.22.07.png

04 map and noremap

ポイント

ショートカットキーを変えるときに
プラグインのコマンドは map を使う
それ以外は noremap を使う

動作

map b a | noremap b a
map c b | noremap c b
map d c | noremap d c
map e d | noremap e d
map f e | noremap f e
map g f | noremap g f
map h g | noremap h g
map x h | noremap x h
 |
x ---> a | x ---> h

プラグインのコマンドかどうか調べる方法

:echo maparg('gx')
<Plug>NetrwBrowseXVis
" <plug> とういのがみえたら map を使うと判断する

実際にやってみる

スペースを2回押したら
グーグルのホームページを表示する

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" スペースを2回押したら
" グーグルのホームページを表示する
" --------------------------------------------------------
syntax enable
filetype plugin indent on
" netrwというプラグインのコマンド gx をスペース2回にマップする
map <Space><Space> gx

vimを立ち上げて グーグルのホームページアドレスを記入する

$ vim -N -u minimal.vimrc foo.foo
 https://www.google.com/

ホームページアドレスの文字の上でスペースを2回押す
Screen Shot 2018-07-11 at 14.44.38.png
ブラウザーが立ち上がってグーグルのホームページが表示される
Screen Shot 2018-07-11 at 14.45.19.png

05 options

左側に行番号を表示する
折畳( fold )をする

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" 左側に行番号を表示する
" 折畳( fold )をする
" --------------------------------------------------------
syntax enable
filetype plugin indent on
let &number = 1
let &foldmethod = 'marker'

vim をたちあげて foo {{{ foo bar baz }}} を記入してみる

$ vim -N -u minimal.vimrc
 foo {{{
 foo
 bar
 baz
 }}}

zc を押す
Screen Shot 2018-07-13 at 3.49.58.png
zo を押す
Screen Shot 2018-07-13 at 3.50.12.png

option の設定詳細

help をみる!

:h option-list
各オプションの簡易説明: *option-list*
'aleph' 'al' Aleph文字(ヘブライ語)用のASCIIコードとする
'allowrevins' 'ari' 挿入モードとコマンドラインモードで、 CTRL-_ を使うことを認める。
'altkeymap' 'akm' デフォルトの第2言語(ペルシア/ヘブライ)
'ambiwidth' 'ambw' Unicode文字の不明瞭な文字幅を決定する
 ...

option のいろいろ

let を使うと綺麗に書ける ( これ重要! )

" Search
let &ignorecase = 1
let &smartcase = 1
let &incsearch = 1
let &hlsearch = 1
let &wrapscan = 1

option がとりうる値

let &number = 1 " boolean
let &tabstop = 4 " number
let &tags = "./tags,tags" " strings

scope

let &tabstop = 2 | set tabstop=2 | " global and local
let &g:tabstop = 2 | setglobal tabstop=2 | " global
let &l:tabstop = 2 | setlocal tabstop=2 | " local

operations for option

" boolean, number and strings
check value | set tabstop? | echo &tabstop
vim default | set tabstop& | nothing
" boolean
set on | set number | let &number = 1
set off | set nonumber | let &number = 0
toggle | set number! | nothing
" number
set value | set tabstop=2 | let &tabstop = 2
" string
add head | set tags^=foo | let &tags = join( [ 'foo', &tags ] , ',' )
add tail | set tags+=foo | let &tags = join( [ &tags, 'foo' ] , ',' )
delete | set tags-=foo | nothing

使っている vim に その option があるかどうか

" 1 is exists, 0 is not exists
echo exists('+tabstop')

option の設定状況 ( :TODO 追記する )

:set
:set all

06 user command

:Hello と入力してリターンすると hello と表示する
Hello コマンドを作成する

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" Hello コマンドを作成する
" --------------------------------------------------------
syntax enable
filetype plugin indent on
command! Hello echo 'hello'

vim を立ち上げて :Hello リターンする

$ vim -N -u minimal.vimrc
 :Hello

Screen Shot 2018-07-12 at 11.21.25.png
hello という文字が下の窓に表示される
Screen Shot 2018-07-12 at 11.20.08.png

parameter for command

:h command-nargs
:h command-completion
:h command-range

07 auto command

Vimを立ち上げたときに下の窓に hello と表示するようにする

minimal.vimrc を下記のようにする

" minimal.vimrc
" --------------------------------------------------------
" Vimを立ち上げたときに
" 下の窓に hello と表示するようにする
" --------------------------------------------------------
" create autocmd group
augroup foo-vimrc
 autocmd!
augroup END
syntax enable
filetype plugin indent on
autocmd! foo-vimrc VimEnter * echo 'hello'

vim を立ち上げる

$ vim -N -u minimal.vimrc

hello という文字が下の窓に表示される
Screen Shot 2018-07-12 at 11.20.08.png

event

:h autocommand-events

08 Folding

vimrc を管理しやすくするための
Folding を使いやすくするするショートカットを設定する

minimal.vimrc を下記のようにする


" minimal.vimrc
" --------------------------------------------------------
" Folding を使いやすくするするショートカット
" --------------------------------------------------------
syntax enable
filetype plugin indent on
let g:mapleader = "\<Space>"
" カーソルの下の fold を open / close
noremap <leader>h zc
noremap <leader>l zo
" すべての fold を open / close
noremap <leader>m zM
noremap <leader>r zR
" カーソルの下の fold を open
" 他の fold を close
noremap <leader>i zMzv

vim を立ち上げて下記のように記入する

$ vim -N -u minimal.vimrc
 foo {{{
 foo
 bar
 }}}
 bar {{{
 foo
 bar
 }}}
 baz {{{
 foo
 bar
 }}}

vim を立ち上げて、入力して スペース m
Screen Shot 2018-07-14 at 0.08.28.png
bar のところで、スペースをおして l を押す
Screen Shot 2018-07-14 at 0.08.45.png
baz のところで、スペースをおして i を押す
Screen Shot 2018-07-14 at 0.09.01.png
スペースを押して r を押す
Screen Shot 2018-07-14 at 0.09.12.png
スペースを押して m を押す
Screen Shot 2018-07-14 at 0.09.24.png

Folding のいろいろ

x foo のような先頭にx がある行を fold する
入力した後 zx もしくは zX と入力すると fold される

function! s:foo(lnum) abort
 return match(getline(a:lnum),'^x\s') + 1
endfunction
let &foldmethod = 'expr'
" thanks @lambdalisue : get(funcref()) idea is good!
let &foldexpr = get(funcref('s:foo'), 'name') . '(v:lnum)'

09 template

いままでの設定をひとつの vimrc にまとめる
ベストプラクティスてきななにか

template.vimrc を下記のようにする

" template vimrc
" prepare {{{1
 " おまじない的ないろいろ
 if &compatible
 let &compatible = 0
 endif
 " encoding
 let &encoding = 'utf-8'
 let &fileencoding = 'utf-8'
 let &fileencodings = 'ucs-boms,utf-8,euc-jp,cp932'
 let &fileformats = 'unix,dos,mac'
 if !has('gui_running') && &encoding !=# 'utf-8'
 let &termencoding = 'utf-8'
 endif
 " vimrc's scriptencoding
 scriptencoding utf-8
 " create autocmd group
 augroup template-vimrc
 autocmd!
 augroup END
" }}}1 -------------------------------------------------
" plugins {{{1
 " プラグインの設定いろいろ・・・
 " turn plugins on
 syntax on
 filetype plugin indent on
" }}}1 -------------------------------------------------
" colorscheme {{{1
 " create highlight group Foo
 autocmd template-vimrc ColorScheme * highlight Foo ctermfg=103 guifg=#8787af
 " set colorscheme
 colorscheme ron
" }}}1 -------------------------------------------------
" options {{{1
 " オプションの設定いろいろ・・・
 let &number = 1
 let &foldmethod = 'marker'
" }}}1 -------------------------------------------------
" key mappings {{{1
 " ショートカットキーの設定いろいろ・・・
 let g:mapleader = "\<Space>"
 " open url
 map <leader><leader> gx
 " useful key mapping for Folding
 noremap <leader>h zc
 noremap <leader>l zo
 noremap <leader>m zM
 noremap <leader>r zR
 noremap <leader>i zMzv
" }}}1 -------------------------------------------------
" file type {{{1
 " ファイルタイプ別の設定いろいろ・・・
 " set filetype foo to *.foo
 autocmd template-vimrc BufNewFile,BufRead *.foo setfiletype foo
 " add pattern 'foofoofoo' to highlight group Foo
 autocmd template-vimrc FileType foo call matchadd( 'Foo', 'foofoofoo' )
" }}}1 -------------------------------------------------
" user command {{{1
 " 自分で作ったコマンドいろいろ・・・
 command! Hello echo 'hello'
" }}}1 -------------------------------------------------
" __END__ {{{
 " おまじない的ないろいろ
 " must be written at the last. see :help 'secure'.
 let &g:secure = 1
 " vim: expandtab softtabstop=2 shiftwidth=2
 " vim: foldmethod=marker
 " vim: foldcolumn=5
" }}}

開いた様子
Screen Shot 2018-07-14 at 0.35.04.png
options のところにカーソルをもってきて、スペースを押して i
Screen Shot 2018-07-14 at 0.35.24.png

10. Vim script

vimrc を書くときに役にたつかもしれない Vim script の最低限の知識

TODO: いつか書く(かもしれない)

11.Reference

sample vimrc

callmekohei's vimrc
ujihisa's vimrc
kaoriya's vimrc

syntax

romainl/Apprentice/color chart
Tweaking Apprentice
Vim のカラースキームが微妙に気に食わないときの対処法

map and noremap

【図解Vim】mapとnoremap

Folding

折り畳み嫌いの男が一夜でFolding freakにまでなった話

template vimrc

vimrcアンチパターン

others

vim-jp 掲示板
vim-users.jp/Hack #45: help を引く

10
5
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
10
5