vimããã¤ãã«ãã
ãã®è¨äºã¯ Vim Advent Calendar 2016 (ãã®2) ã®3æ¥ç®ã®è¨äºã§ãã
ãã¼ãã«ã¢ã¼ã以å¤ããã¤ãã¨ãã¦ä½¿ãéã®æ å ±ã追è¨ãã¾ãã (2016/12/04)
UNIXã®ããã¹ãå¦ç
UNIXã§ããã¹ããèªåæ´å½¢ããéããã¤ãæ©è½ã¯æ¬ ãããªãã
$ cat a.txt 1 hoge 2 piyo 3 fuga $ cat a.txt |sed 's/piyo/foo/' |grep '2' 2 foo
ã·ã§ã«ä¸ã§ |
ã¨ãããã¤ãè¨å·ã使ã£ã¦ã³ãã³ãã次ã
ç¹ãããã¨ã§ãè¤éãªããã¹ãå¦çãããªãããã ã
ãã¤ãã¯ä¾¿å©ã ããããã¹ãã¨ãã£ã¿ããã¤ãã¨ãã¦ä½¿ã人ã¯ãã¾ãè¦ãããªãã
ããã¹ãã¨ãã£ã¿=対話ç ã¨ãã常èãããããããã¤ãã®ãããªèªåå¦çã¨ã¯ç¸æ§ãæªãã¨æããã¦ããã®ã ããã
ãããä»æ¥ã¯ããã¦ãã·ã§ã«ã¹ã¯ãªãããã¯ã³ã©ã¤ãã¼ã®ä¸ã«vimãåãè¾¼ã¿ããã¤ãã¨ãã¦éçã«ä½¿ã£ã¦ã¿ããã
vimããã¤ãã¨ãã¦ä½¿ã
vimãæ®éã«ä½¿ã£ã¦åé ã®$ cat a.txt |sed 's/piyo/foo/'
ãè¡ãã«ã¯ããã¼ãã«ã¢ã¼ãã§jwCfoo
ã¨å
¥åããã°ããã
ãã®jwCfoo
ã¨ããåªæãã·ã§ã«ä¸ã§ä½¿ç¨ããã«ã¯ä»¥ä¸ã®ããã«ããã°è¯ãã
# æãæ®éã®æ¹æ³ $ cat a.txt |vim -es +'norm jwCfoo' +%p +q! /dev/stdin # ããã㯠$ cat a.txt |ex -s +'norm jwCfoo' +%p +q! /dev/stdin # ãã®ããæ¹ã¯ Vim: Reading stdin... åé¡ãçãã $ cat a.txt |vim - -es +'norm jwCfoo' +%p +q! |sed '1d'
ã¨ããã°ãããä¸çªä¸ã®ã¯ã³ã©ã¤ãã¼ã®èª¬æããã¦ããã¨
vim -e
ã¯ex
ã¨ç価ã§ãvimãé対話(exã¢ã¼ã)ã§èµ·åããã¨ããæå³ã-s
ã¯ãµã¤ã¬ã³ãã¢ã¼ãã§èµ·åããæ¨æºåºåãæ±ããªãã¨ãããªãã·ã§ã³ã+'norm jwCfoo'
ã®norm
ã¯ããã¼ãã«ã¢ã¼ãã®ã³ãã³ãã使ãã¨ããæå³ãjwCfoo
ã¯vimã®åªæãä¸ã«ESCãå ¥ããã«ã¯Ctrl+vãã¦ESCãæ¼ãã+%p
ã¯ãã¡ã¤ã«ã®å ¨å 容ãæ¨æºåºåã«è¡¨ç¤ºããexã³ãã³ãã+q!
ã¯vimãå¼·å¶çµäºããexã³ãã³ãã馴æã¿æ·±ãã/dev/stdin
ã¯èªã¿è¾¼ã¿å ãæ¨æºå ¥å (cat a.txt) ã«ããã¨ããæå³ã
ä¸çªç®ã®ã¯ã³ã©ã¤ãã¼ã¯/dev/stdin
ã®ä»£ããã«ãã¤ãã³(æ¨æºå
¥åã®ã·ã³ãã«)ã使ã£ã¦ãã¦ãã¹ãã¼ãã«è¦ããã
ãã ããã®æ¹æ³ã§ã¯ãæ¨æºåºåã®åé ã«Vim: Reading from stdin...
ã¨ããã¯ãã½ãããæååãåæã«æ¿å
¥ãããã
ããã¤ãæ¶ãã«ã¯ãç´å¾ã«ã·ã§ã«ã³ãã³ã|sed '1d'
çãåã¾ãå¿
è¦ãããã
å¤é¨ã³ãã³ãã«ããã
以ä¸ã®ãããªvipeã³ãã³ããä½ã£ã¦ããã¨ãã·ã§ã«ä¸ã§vimã®åªæã使ãããããªãã
# .bashrcã.zshrcã«æ¸ãè¾¼ã vipe () { COMMAND=$(echo "$*") # ã³ãã³':'ã§ESCå ¥åã代æ¿ããå ´åã¯ã³ã¡ã³ããå¤ãã^[ã¯Ctrl+vãã¦ESCæ¼ãã¦å ¥å # COMMAND=$(echo "$*" |sed -e 's/:/^[/g') vim - -es +":norm gg" +":norm $COMMAND" +:%p +:q! |sed '1d' }
使ãæ¹ã¯ä»¥ä¸ã®ãããªæãã
$ cat a.txt 1 hoge 2 piyo 3 fuga $ cat a.txt |vipe jwCfoo |grep '2' 2 foo # ^[ã¯ESCæåã§ãCtrl+Vããå¾ã«ESCãæ¼ãã¦å ¥åãã $ cat a.txt |vipe Abar^[oxxxx 1 hogebar xxxx 2 piyo 3 fuga # ã³ãã³ãã«ç©ºç½æåãå«ãå ´åã¯'ã"ã§ããã $ cat a.txt |vipe "A bar baz" 1 hoge bar baz 2 piyo 3 fuga
ã·ã§ã«ä¸ã§vimã®ãã¯ãæ©è½ã使ã
vimã«ã¯è¶
便å©ãªãã¯ãæ©è½ãåå¨ããã
ç°¡åã«èª¬æããã¨ããã¼ãã«ã¢ã¼ãã«ãããä¸é£ã®æä½ããã¯ãæååã¨ãã¦è¨é²åçããæ©è½ã§ã
qa
ã¨æ¼ããã¨ã§ã¬ã¸ã¹ã¿aã«è¨é²ãã@a
ã¨æ¼ããã¨ã§åçã§ããã
ä¸è¿°ã®jwCfoo
ã¨ããåªæã¯vimã®ãã¯ãã®ä¸ç¨®ã¨ãããã ããã
vimã対話çã«ä½¿ã£ã¦ (qa
ã使ã£ã¦) è¨é²ãããã¯ãã表示ããã«ã¯ããã¼ãã«ã¢ã¼ãã§"ap
ã¨æ¼ãã°è¯ãã
ãããã¦è¡¨ç¤ºãããã¬ã¸ã¹ã¿aã®ä¸èº«ã¯ãvipeã³ãã³ãã®å¼æ°ã¨ãã¦é£ããããã¨ãåºæ¥ãã
$ cat a.txt |vipe "ã¬ã¸ã¹ã¿ã®ä¸èº«ãæ¸ãè¾¼ã"
ããã§ãvimã®ãã¯ãã使ãã¾ãããããªãããªãã¦å ´åãå®å¿ã§ããï¼
åè
vim -es
ã¨ããæã«è¡¨ç¤ºãããVim: Reading from stdin...
åé¡ã«ã¤ãã¦ã¯ã³ãã©ã
vimã®ã½ã¼ã¹ã³ã¼ããä¿®æ£ãã¦ããï½
- http://vi.stackexchange.com/questions/4682/how-can-i-suppress-the-reading-from-stdin-message-from-within-vim
- http://vim.1045645.n5.nabble.com/Vim-Reading-from-stdin-td1151013.html
追è¨
ãã³ã¡ã§è¯ãææãããã ãã¾ããã
vimããã¤ãã«ãã - ä½ç½ã®æ¸ããªããVimããã¤ãã§ä½¿ãã®ãã£ã¡ã便å©ï¼ãã ããªãã§normalã³ãã³ãéå®ã®æ¸ãæ¹ã«ãããã ãï¼
2016/12/04 01:23
確ãã«è¨äºä¸ã§ã¯ãã¤ãã®å¯¾è±¡ãvimã®ãã¼ãã«ã¢ã¼ãã«éå®ãã¦ãã¾ããã ããvimã®ãã¸ã¥ã¢ã«ã¢ã¼ãã使ããããã°ã以ä¸ã®ããã«vãå ¥ããã°è¯ãã§ãã
$ cat a.txt 1 hoge 2 piyo 3 fuga $ cat a.txt |vipe lvjhd 1 piyo 3 fuga
ç©å½¢é¸æã使ããããã°ãvã®ä»£ããã«Ctrl+vãäºåé£æããã°è¯ãã§ããã
ããvimã®ã³ãã³ãã©ã¤ã³ã¢ã¼ãã使ã£ã¦ããã¹ãã®æ´å½¢ãããããã°...ã¾ã¦ããvimã®è¦ªã¯viã§ãã®è¦ªã¯edã§ãã¹ããªã¼ã çã®edã¯sed... ãªã®ã§ãvimã®åç¶ã«ãããsedã使ãã¨è¯ãã§ãããã
ã³ãã³ãã©ã¤ã³ã¢ã¼ãã使ã£ããã¡ã¤ã«ã®ä½æãæ¸ãè¾¼ã¿ã¯ãvipeã§ããããå®è£ ãããããã·ã§ã«ã®ãªãã¤ã¬ã¯ã·ã§ã³æ©è½ã使ãæ¹ãè¯ãã¨æãã¾ãã ãã©ããã¦ãvipeã§ãã¡ã¤ã«ãä¸æ¸ãããããããããã¨ããæ¹ãããã£ãããã°ã
vim - -es +":norm gg" +":norm $COMMAND" +:%p +:q! |sed '1d'
ã®:q!
ã®é¨åã":wq! hoge.txt"
ã«å¤æ´ãããªã©ããã¾ã調æ´ãã¦ãã ããï½