æµè¡ã£ã¦ãã¿ãããªã®ã§é ãã°ããªãã便ä¹ãéè¦?ä½ããããããã®?
- Perl基礎文法最速マスター - サンプルコードによるPerl入門 - Perlプログラミングの基礎から応用まで
- Route 477(2010-01-25)
- PHP基礎文法最速マスター - Shin x blog
- Python基礎文法最速マスター - LazyLife@Diary
- VBA基礎文法最速マスター - いろいろ解析日記
- Java基礎文法最速マスター - いろいろ解析日記
- Bash基礎文法最速マスター - いろいろ解析日記
- Haskell基礎文法最速マスター - think and error
- Brainf*ck基礎文法最速マスター - このブログは証明できない。
- VBScript 基礎文法最速マスター - CX's VBScript Diary - VBScript グループ
- JavaScript基礎文法最速マスター - gifnksmの雑多なメモ
以ä¸ãVim 7.2 ãåæã¨ãã¾ãã
1. åºç¤
å ¨ã¦ã³ãã³ã
Vimã¹ã¯ãªããã¯ã³ãã³ãã®ç¾ åã§ããåç´ãªä»£å ¥ãé¢æ°ã®å¼ã³åºããif ãªã©ã®å¶å¾¡æ§æãå ¨ã¦ã³ãã³ãã§ãã
echo ã³ãã³ã
echo 'Hello, world!'
:echo ã³ãã³ãã®åºåã«ã¯å¿ ãæ¹è¡ãå«ã¾ãã¾ããæ¹è¡ãå«ã¿ãããªãå ´å㯠:echon ã³ãã³ãã使ãã¾ãã
echon 'Hello, ' echon 'world!'
ã³ã¡ã³ã
" ããè¡æ«ã¾ã§ãè¡ã³ã¡ã³ãã§ãã
" ã³ã¡ã³ã
Vimã®ãã¢ãå¶ç´ã«ãããã³ãã³ãã«ãã£ã¦ã¯ã³ã¡ã³ãã使ããªãå ´åãããã¾ã*1ã
ä»£å ¥
å¤ã®ä»£å ¥ã«ã¯ :let ã³ãã³ãã使ãã¾ãã
let a = 1
ãã¼ã¿ã®å
ãã¼ã¿ã«ã¯åãããã以ä¸ã®ããããã§ãã
- æ´æ°å¤
- å°æ°å¤
- æåå
- ãªã¹ã
- è¾æ¸
- é¢æ°åç §
é¢æ°åç §ã¯ãã®è¨äºã§ã¯çç¥ãã¾ãã
å®è¡
å®è¡ããã«ã¯ :source ã³ãã³ãã使ãã¾ãã
source script.vim
ã·ã§ã«ããå®è¡ããå ´å㯠-S ãªãã·ã§ã³ã使ãã¾ãã
vim -S script.vim
2. æ°å¤
æ°å¤ã®è¡¨ç¾
æ°å¤ã«ã¯æ´æ°ã¨å°æ°ãããã¾ããå°æ°ã¯ Vim 7.2 ããå°å ¥ããã¾ããã
let num = 1 let num = 1.234 let num = 12.0e-3
ååæ¼ç®
ååæ¼ç®ã§ãã
let num = 1 + 1 let num = 1 - 1 let num = 1 * 2 let num = 1 / 2
+ 㨠- ã®ã¿è¤åæ¼ç®åã使ãã¾ãã
let num += 1 let num -= 1
*= ã /= ã¯ä½¿ããªãã®ã§æ³¨æãã¦ãã ããã
å°ä½ãããã¾ãã
let num = 3 % 2
Vim ã®å°æ°ã¯å¾ä»ãã®ããæ´æ°å士ãå²ãç®ãã¦ãçµæã¯æ´æ°ã®ã¾ã¾ã§ãã
let num = 3 / 2 " => 1 let num = 3 / 2.0 " => 1.5
3. æåå
æååã®è¡¨ç¾
æååã¯ã·ã³ã°ã«ã¯ã©ã¼ããããã«ã¯ã©ã¼ãã§å²ã¿ã¾ãã
ããã«ã¯ã©ã¼ãã®ä¸ã§ã¯\t(ã¿ã)ã\n(æ¹è¡)ãªã©ã®ç¹æ®æåãå©ç¨ãããã¨ãã§ãã¾ãã
ã·ã³ã°ã«ã¯ã©ã¼ãå
ã§ã¯ã·ã³ã°ã«ã¯ã©ã¼ãèªèº«ã表ç¾ããã®ã«ã·ã³ã°ã«ã¯ã©ã¼ãã 2 ã¤éãã¾ãããã以å¤ã®æåã¯ä¸åå±éããã¾ããã
let str = "abc" let str = 'def' let str = "abc\ndef" let str = 'That''s right!'
æååæä½
å種æååæä½ã§ãã
" çµå let join1 = 'aaa' . 'bbb' let join2 = join(['aaa', 'bbb', 'ccc'], ',') " åå² let record = split('aaa,bbb,ccc', ',') " é·ã let length = strlen('abcdef') " åãåºã let substr = 'abcd'[1] " b let substr = 'abcd'[0 : 1] " ab let substr = 'abcd'[ : 1] " ab let substr = 'abcd'[2 : ] " cd let substr = 'abcd'[1 : -1] " bcd " æ¤ç´¢ let result = stridx('abcd', 'cd') " è¦ã¤ãã£ãå ´åã¯ãã®ä½ç½®ãè¦ã¤ãããªãã£ãå ´åã¯-1ãè¿ã
4. ãªã¹ã
ãªã¹ãã®è¡¨ç¾
let list = [1, 2, 3]
ãªã¹ãã®è¦ç´ ã®åç §ã¨ä»£å ¥
let list = [1, 2, 3] let e0 = list[0] " => 1 let e2 = list[-1] " => 3 let e1_2 = list[1 : 2] " => [2, 3] let e_1 = list[ : 1] " => [1, 2] let list[0] = 10 let list[1] = 20 " => list == [10, 20, 3]
ãªã¹ãæä½
ãªã¹ããæä½ããé¢æ°ã§ãã
let list = [1, 2, 3] " len(ãªã¹ãã®é·ã) let length = len(list) " 3 " remove(ä»»æã®è¦ç´ ãåãåºã) let first = remove(list, 0) " first == 1, list = [2, 3] let last = remove(list, -1) " last == 3, list = [2] " insert(ä»»æã®ä½ç½®ã«è¦ç´ ã追å ) " (åç´ãªé¢æ°ã®å¼ã³åºãã«ã¯ :call ã³ãã³ãã使ãã¾ãã) call insert(list, 5, 0) " list == [5, 2] " add(æ«å°¾ã«è¦ç´ ã追å ) call add(list, 9) " list == [5, 2, 9]
5. è¾æ¸
è¾æ¸ã¯ãä»ã®è¨èªã§ããã·ã¥ãã¼ãã«ãé£æ³é åã¨å¼ã°ãã¦ãããã¼ã¿æ§é ã§ãããã¼ã«ã¯æååã®ã¿ä½¿ç¨ã§ãã¾ãã
è¾æ¸ã®è¡¨ç¾
let dict = {} " 空ã®è¾æ¸ let dict = {'a': 1, 'b': 2}
è¾æ¸ã®è¦ç´ ã®åç §ã¨ä»£å ¥
let a = dict['a'] let b = dict.b let dict['a'] = 10 let dict.c = 30
è¾æ¸æä½
" å ¨ã¦ã®ãã¼ããªã¹ãã§åå¾ let keylist = keys(dict) " å ¨ã¦ã®å¤ããªã¹ãã§åå¾ let valuelist = values(dict) " å ¨ã¦ã®ãã¼ã¨å¤ã [[key, value], ...] ã®å½¢å¼ã®ãªã¹ãã§åå¾ let itemlist = items(dict) " ãã¼ã®åå¨ç¢ºèª let exists = has_key(dict, 'a') " ãã¼ã®åé¤ call remove(dict, 'a')
6. å¶å¾¡ã³ãã³ã
if ã³ãã³ã
if æ¡ä»¶ endif
if ã elseif ã³ãã³ã
ä»ã®è¨èªã®ããã« else if ã§ã¯ãªã elseif ã§ãããã¨ã«æ³¨æãã¾ãããã
if æ¡ä»¶ elseif æ¡ä»¶ endif
while ã³ãã³ã
let i = 0 while i < 5 " å¦ç let i += 1 endwhile
for ã³ãã³ã
for ã¯ãªã¹ãã®ä¸èº«ãèµ°æ»ããã³ãã³ãã§ãã
for e in [1, 2, 3] echo e endfor
7. ãã®ä»ã®æ¼ç®å
ããã¾ã§ã§åºã¦ãã¦ããªãæ¼ç®åã§ãã
æ¯è¼æ¼ç®å
- ==
- !=
- >
- >=
- <
- <=
ãããã®æå³ã¯ä¸è¬çãªè¨èªã¨åæ§ã§ããæ°å¤å士ã®å ´åã¯æ°å¤æ¯è¼ãæååå士ã®å ´åã¯æååæ¯è¼ã«ãªãã¾ãã
æååã¨æ°å¤ãæ¯è¼ããå ´åãæååãæ°å¤ã«å¤æãããæ°å¤ã¨ãã¦æ¯è¼ããã¾ãã
æååå士ãæ¯è¼ããå ´åã大æåã¨å°æåãåºå¥ããã㯠Vim ã® 'ignorecase' ãªãã·ã§ã³ã«å·¦å³ããã¾ãããã®ãªãã·ã§ã³ãç¡è¦ãã¦æ¯è¼ããã«ã¯ä»¥ä¸ã®æ¼ç®åã使ãã¾ãã
- 大æåå°æåãåºå¥ãã
- ==#
- !=#
- >#
- >=#
- <#
- <=#
- 大æåå°æåãç¡è¦ãã
- ==?
- !=?
- >?
- >=?
- <?
- <=?
æ£è¦è¡¨ç¾ããããããã«ã¯ä»¥ä¸ã®æ¼ç®åã使ãã¾ãã
- æ£è¦è¡¨ç¾ããã
- =~
- =~#
- =~?
- æ£è¦è¡¨ç¾éããã
- !~
- !~#
- !~?
æ¯è¼æ¼ç®åã®ã¾ã¨ãã§ãã
'ignorecase'次第 | 大å°æåèæ ® | 大å°æåç¡è¦ | |
---|---|---|---|
çãã | == | ==# | ==? |
çãããªã | != | !=# | !=? |
ãã大ãã | > | ># | >? |
ãã大ãããçãã | >= | >=# | >=? |
ããå°ãã | < | <# | <? |
ããå°ãããçãã | <= | <=# | <=? |
æ£è¦è¡¨ç¾ããã | =~ | =~# | =~? |
æ£è¦è¡¨ç¾éããã | !~ | !~# | !~? |
== 㨠!= ã¯ãªã¹ããè¾æ¸ã®åå¤æ§ãã¹ãã«ä½¿ãã¾ãã
let res = [1, 2, 3] == [1, 2, 3] " 1
is 㨠isnot æ¼ç®åã¯ãæ°å¤ãæååã®å ´åã¯åå¤æãè¡ããªãå³æ ¼ãªæ¯è¼ããã¾ãããªã¹ããè¾æ¸ã®å ´åã¯åä¸æ§ãã¹ãããã¾ãã
let res = 1 is '1' " 0 let res = 1 is 1 " 1 let list = [0, 1] let res = list is [0, 1] " 0 let res = list is list " 1
è«çæ¼ç®å
ä¿ã«è¨ãç絡æ¼ç®åãã·ã§ã¼ããµã¼ãããæ¼ç®åã§ããçµæã¯çå½å¤ã表ã 0 ã 1 ã«å¤æããã¾ãã
let a = 10 || 0 " a == 1 let a = 10 && 5 " a == 1 let a = 10 && 0 " a == 0
æ¡ä»¶æ¼ç®å
ä¸è¬çã«ã¯ä¸é æ¼ç®åã¨ãå¼ã°ãã¦ãã¾ãã
echo cond ? 'true' : 'false'
8. é¢æ°
é¢æ°ã¯ :function ã³ãã³ãã§å®ç¾©ãã¾ããã¦ã¼ã¶å®ç¾©ã®é¢æ°åã¯çµã¿è¾¼ã¿é¢æ°åã¨åºå¥ããããã«å¤§æåã§å§ããå¿ è¦ãããã¾ãã
function! Sum(v1, v2) return a:v1 + a:v2 endfunction
- function! ã® ! ã¯ãååã®é¢æ°ãåå®ç¾©ããéã«æ¢åã®é¢æ°ãä¸æ¸ããããã¨ã示ãã¾ããã¹ã¯ãªãããåèªã¿è¾¼ã¿ããæã®ããã«ãåºæ¬çã«ä»ãã¦ãããæ¹ãè¯ãã§ãããã
- é¢æ°å ã§å¼æ°ã«ã¢ã¯ã»ã¹ããã«ã¯ãa: ã¨ããæ¥é åãå¿ è¦ã§ãã
åä¸ã¹ã¯ãªããå ã§ã®ã¿ä½¿ããé¢æ°(ã¹ã¯ãªãããã¼ã«ã«ãªé¢æ°)ãå®ç¾©ããã«ã¯ãé¢æ°åã®å é ã« s: ãä»ãã¾ãããã®å ´å s: 以éã®é¢æ°åã¯å¤§æåã§å§ããå¿ è¦ã¯ããã¾ããã
function! s:sum(v1, v2) return a:v1 + a:v2 endfunction
å¯å¤é·å¼æ°ã使ãããå ´åã¯å¼æ°ã®æå¾ã ... ã«ãã¾ããå¯å¤é·å¼æ°ã«ã¢ã¯ã»ã¹ããã«ã¯ä»¥ä¸ã®ããã«ãã¾ãã
- a:0
- å¯å¤é·é¨åã®å¼æ°ã®æ°
- a:n
- n ã¯æ°åãn çªç®ã®å¼æ°ã«ã¢ã¯ã»ã¹ãããa:1 a:2 ãªã©ã
- a:000
- å¯å¤é·é¨åã表ããªã¹ããã¶ã£ã¡ããããã使ãã°åè ã® 2 ã¤ã¯ãªãã¦ããªãã¨ããªãã
function! s:sum(v1, ...) let num = a:v1 for i in a:000 let num += i endfor return num endfunction
9. ãã¡ã¤ã«å ¥åºå
Vim ã®ãããã¡ãçµç±ããæ¹æ³ãããã¾ãããããã§ã¯ Vim ã®ç°å¢ã«å½±é¿ãä¸ããªãæ¹æ³ã示ãã¾ãã
readfile() é¢æ°
ãã¡ã¤ã«ã®ä¸èº«ãè¡åºåãã®ãªã¹ãã§åå¾ãã¾ãã
for line in readfile(fname) if line =~ 'Date' echo line endif endfor
writefile() é¢æ°
è¡åºåãã®ãªã¹ãããã¡ã¤ã«ã«æ¸ãåºãã¾ãã
call writefile(['line1', 'line2', 'line3'], fname)
ç¥ã£ã¦ãããæ¹ãããææ³
çå½å¤
:if :while ãæ¡ä»¶æ¼ç®åçã®çå½å¤ãæ±ããããå ´æã§ã¯ãå¤ã¯æ°å¤ã«å¤æããããã®å¤ã 0 ããã以å¤ãã§å¤å®ããã¾ãã0 ã®æã¯å½ããã以å¤ã®æã¯çã§ãã
å¤ã®å¤æ
å
ã»ã©ã®çå½å¤ããæååã®é£çµãæ°å¤æ¼ç®ãªã©ç¹å®ã®ç®æã§ã¯å¤ã¯æé»çã«é©åãªå¤ã«å¤æããã¾ãã
æ´æ°å¤ãæååã«ãªãå ´åã¯ã10 é²æ°è¡¨è¨ã®æååã«ãªãã¾ãã
æååãæ´æ°å¤ã«ãªãå ´åã¯ããªãã©ã«è¡¨è¨ã¨åæ§ã«è§£éããã¾ããæååã®æ«å°¾ã«ã´ãããã£ã¦ãç¡è¦ããã¾ããå¤æã§ããªãå ´å㯠0 ã«å¤æããã¾ãã
ãã以å¤ã®æé»ãªå¤æã¯è¡ããã¾ãããæ´æ°å¤ã¨å°æ°å¤ã®å¤æãæé»çã«ã¯è¡ãããªãã®ã§æ³¨æãã¦ãã ããã
å¤æãå¿
è¦ãªå ´é¢ã§åã®å¤æãã§ããªãå ´åã¯ã¨ã©ã¼ã«ãªãã¾ãã
å¤ã®åä»£å ¥
ãã§ã«ããå¤æ°ã«å¤ã代å
¥ããå ´åãå
ã®å¤ã¨æ°ããå¤ãåå¤æå¯è½ãªåã§ãªãå ´åã¯Vim ã¯èéãå©ããªãã®ã§ã¨ã©ã¼ã«ãªãã¾ãã
ãã®å ´å㯠:unlet ã³ãã³ãã使ã£ã¦ä¸æ¦å¤æ°ãåé¤ããå¿
è¦ãããã¾ãã
let a = [1,2,3] "let a = 1 " ã¨ã©ã¼ï¼ unlet a " ä¸æ¦ unlet ããå¿ è¦ããã let a = 1
ããã¯ç¹ã«æ§ã ãªå¤ãå ¥ã£ããªã¹ãã :for ã§åãéã«æ³¨æãå¿ è¦ã§ãã
for i in [0, 1.1, ['list'], 'string'] echo i unlet i " i 㯠for ã®é¨åã§åä»£å ¥ãããã®ã§ unlet ãå¿ è¦ endfor
å¤æ°ãå®ç¾©ããã¦ãããã©ããã®å¤å®
exists() é¢æ°ã使ãã¾ãã
let e = exists('var1')
便å©ãªé¢æ°
- range()
- é£çªã®ãªã¹ããçæãã¾ãã:for ã¨çµã¿åãããã¨å¹æçã§ãã
- range(3) == [0, 1, 2]
- range(1, 3) == [1, 2, 3]
- range(1, 5, 2) == [1, 3, 5]
- map(expr, string)
- 第ä¸å¼æ°ã®ãªã¹ãã®åè¦ç´ ã string ã§æ¸¡ããæååãè©ä¾¡ããå¤ã«å¤æãã¾ããv:val ãè¦ç´ ã¨ãã¦åç §ã§ãã¾ãã
- è¾æ¸ã渡ãã¾ããè¾æ¸ã®å ´åã¯è¾æ¸ã®åå¤ãå¤æãããä»ãv:key ã§ãã¼ãåç §ã§ãã¾ãã
- 渡ãããªã¹ãèªä½ãæ¸ãæããããã®ã§æ³¨æãã¦ãã ãããã³ãã¼ã¯ä½ããã¾ããã
- map([1, 2, 3], 'v:val + 1') == [2, 3, 4]
- filter(expr, string)
- 第ä¸å¼æ°ã®ãªã¹ãã®åè¦ç´ ã string ã§æ¸¡ããæååãè©ä¾¡ããçµæãçã«ãªããã®ã®ã¿ã«ãªãããã«ãã£ã«ã¿ãªã³ã°ãã¾ããv:val ãè¦ç´ ã¨ãã¦åç §ã§ãã¾ãã
- è¾æ¸ã渡ãã¾ããè¾æ¸ã®å ´åã¯è¾æ¸ã®åå¤ãè©ä¾¡ãããä»ãv:key ã§ãã¼ãåç §ã§ãã¾ãã
- 渡ãããªã¹ãèªä½ãæ¸ãæããããã®ã§æ³¨æãã¦ãã ãããã³ãã¼ã¯ä½ããã¾ããã
- filter([1, 2, 3], 'v:val % 2 == 1') == [1, 3]
ãªã¹ãä»£å ¥
ãªã¹ããè¤æ°ã®å¤æ°ã«ã°ããã¦ä»£å ¥ã§ãã¾ããä»£å ¥å ã®å¤æ°ã®æ°ã¨ä»£å ¥å ã®ãªã¹ãã®é·ãã¯åãã§ããå¿ è¦ãããã¾ãã
let [key, value] = ['key', 'value'] let [e1, e2; rest] = ['e1', 'e2', 'rest1', 'rest2']
ä¾å¤å¦ç
å°ç¨ã®ã³ãã³ã群ãããã¾ãã
try throw "ã¨ã©ã¼ã¡ãã»ã¼ã¸" catch /error/ " ã¨ã©ã¼ã¡ãã»ã¼ã¸ã«ãããããæ£è¦è¡¨ç¾ catch " 空ã®å ´åã¯å¿ ãããããã " ... ä¾å¤å¦ç ... finally " å¿ ãè¡ãããå¦ç endtry
åèè³æ
å ¨ã¦ :help ã«æ¸ãã¦ããã¾ããWeb ä¸ã®æªããè³æ*2ã«ã¯èª¤ã£ããã®ãå¤ããã®ãå¤ãã®ã§ãå¯è½ãªéã :help ãåç §ãã¾ãããã
Vimã¹ã¯ãªãããããã°ã©ãã³ã°è¨èªã® 1 種ã¨ãã¦è¦ãå ´åã®ä½¿ãæ¹ã«ã¤ãã¦ã網ç¾
çã¨ã¯è¨ãã¾ãããç°¡åã«ã¾ã¨ãã¦è¦ã¾ããããåç¥ã®éã Vimã¹ã¯ãªãã㯠Vim ãæ¡å¼µããããã®è¨èªãªã®ã§ãVim ã®ç¶æ
ã«ã¢ã¯ã»ã¹ãæä½ããæ¹æ³ãç¥ããªãã¨å®ã¯ãã¾ãå½¹ã«ç«ã¡ã¾ããã
ãã ãããã¾ã§æ¸ãã¦ãã¨ã²ã©ãããªã¥ã¼ã ã«ãªãããã ã£ãã®ã§ä»åã¯çç¥ãã¾ããã