@peccul is peccu

(love peccu '(emacs lisp cat outdoor bicycle mac linux coffee))

Emacsで置換しながらカウントアップする

テストケースの連番を書くのがめんどくさいので調べた。 便利すぎる。

f:id:peccu:20151210220546p:plain

tl;dr

C-M-% _004 RET _\,(format "%03d. " (+ 4 \#))

例えばこんなテストケースがある。

-(void)test_someMethod_001{
  // test
}
-(void)test_someMethod_002{
  // test
}
-(void)test_someMethod_003{
  // test
}
-(void)test_someMethod_004{
  // test
}

4番をコピペした

-(void)test_someMethod_001{
  // test
}
-(void)test_someMethod_002{
  // test
}
-(void)test_someMethod_003{
  // test
}
-(void)test_someMethod_004{
  // test
}
-(void)test_someMethod_004{
  // test
}
-(void)test_someMethod_004{
  // test
}
-(void)test_someMethod_004{
  // test
}

M-x query-replace-regexで置換する

C-M-% _004 RET _\,(format "%03d. " (+ 4 \#))

-(void)test_someMethod_001{
  // test
}
-(void)test_someMethod_002{
  // test
}
-(void)test_someMethod_003{
  // test
}
-(void)test_someMethod_004{
  // test
}
-(void)test_someMethod_005{
  // test
}
-(void)test_someMethod_006{
  // test
}
-(void)test_someMethod_007{
  // test
}

イメージ図

anzuを入れていると、リアルタイムに置換結果が確認できてテンション上がる。

f:id:peccu:20151210220546p:plain

解説

置換文字列で\#が何個目の置換かを保持していて、さらにS式を書くこともできるのでオフセットをずらしたり、a,b,cにしたりできる。

  • 参考

emacs-fu: replace-regexp and numbering lines

GNU Emacs Manual: Regexp Replace