Skip to content

Instantly share code, notes, and snippets.

@2nd-player
Last active August 11, 2017 06:19
Show Gist options
  • Save 2nd-player/4875857cadc54d4d95a3a0ecf0a1629f to your computer and use it in GitHub Desktop.
Save 2nd-player/4875857cadc54d4d95a3a0ecf0a1629f to your computer and use it in GitHub Desktop.
sum up integers in the ed text editor
1,2g/[^0-9]/s///g
1,2g/^00*/s///g
1s/$/!/
2s/$/__/
1,2s/\(.\)/\1\
/g
1t0
1d
1,/_/g/^ *$/d
1,/_/g/^/m0
1d
1,/_/t0
1t0
1d
1,/_/j
1g/ /s/ //g
1s/_$/\
@/
1t0
1d
/@/,/_/g/[0-9]/1s/^\([0-9]\)\([0-9]*\)!\([0-9]\)\([0-9]*\)/\2!\4\
\1\3/
1s/!//
1s/$/!/
1s/\(.\)/\1\
/g
1t0
1s/.*//g
1,/!/g/^/m0
1d
1,/@/g/^ *$/d
1,/@/s/^/ /
/@/s/$/\
@/
1t0
1d
1,/@/j
1t0
2s/.*//
/@/,/_/g/^/2s/$/ IIIIIIIIII/
2s/^/!/
1g/ */s// /g
1g/^ */s// /
1,2j
1s/ *@ *//
1g/9/s//IIIIIIIII/g
1g/8/s//IIIIIIII/g
1g/7/s//IIIIIII/g
1g/6/s//IIIIII/g
1g/5/s//IIIII/g
1g/4/s//IIII/g
1g/3/s//III/g
1g/2/s//II/g
1g/1/s//I/g
1g/0/s///g
/@/,/_/g/[0-9]/1s/ IIIIIIIIII/I /
1s/^ */ /
1s/!.*//
1g/ IIIIIIIII/s//9/g
1g/ IIIIIIII/s//8/g
1g/ IIIIIII/s//7/g
1g/ IIIIII/s//6/g
1g/ IIIII/s//5/g
1g/ IIII/s//4/g
1g/ III/s//3/g
1g/ II/s//2/g
1g/ I/s//1/g
1g/ /s//0/g
1s/^0*//
2,/_/d
1p
wq /dev/null
q!
" sum up integers in first two lines and print it
" compatible with ed, ex and nvi.
" author: 2nd-player (pokemonwiki.com)
" エディタはエディタに過ぎない: do one thing wellってのが
" Unix philosophy だとは思いつつ, ちょっと反発してみたくなった.
" Unix的にはbcとかexprとか使うのが多分正しい
:r !expr 5 + 16
" 外部コマンドの使用を解禁したとしても,
" edからこういう計算を自動化する場合は一時ファイルが必要.
"" そういう処理はシェルスクリプトでやろうよって話.
"" edはTECOと違ってスクリプト言語では無い
" == edスクリプトtips ==
" global//はforeach的にループする
" s///はマッチしないとエラーで止まってしまう.
"" 事前にs/$/dummy/とか埋め込む
"" gコマンドでテスト: 1g/pattern/s//replacement/
" Vimにはこの制約はないが, substを使って改行したいときの
" エスケープ方法に互換性が無い? 何とかならんのかこれ
"" Vimだったらそもそも普通に算術演算できるけどね
:exe append(0, getline(1) + getline(2))
" joinのときに入る空白の数は環境依存.
" edとexの両方で"保存せずに終了"と解されるコマンドは無い
" :q! ... edで Unexpected commmand suffix
" :e! ... edで Unexpected com(ry
" :q ... ex系で No write sin(ry
" :E ... ex系で Not an editor command
" :Q ... ex系で Not an ed(ry
" nviで警告出るけどedとexで通ったので
" とりあえず今回は /dev/null に捨てる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment