About me

  • Kenta Usami a.k.a. @tadsan
    • Usami is my family name, call me Kenta or /たっ?どさん/.
  • Current maintainer of Emacs PHP Mode
  • I am Vim beginner since 2011
  • I am Emacs user since 2013
  • I am PHPer, Emacs Lisper and language hobyist
  • I am member of Emacs JP community
% cat ~/.zsh_history | grep -v cd | grep -Eo 'emacs|vim|micro' | sort | uniq -c | sort -nr
   2226 vim
     78 emacs
     64 micro

Vim is awesome!

Background of this presentation

  • I respect my colleague who has done complex refactoring with Vim script many times
  • It is possible to write text processing in Lisp and run it in Emacs, but it is more verbose than Vim
  • In my opinion, Vim is more efficient and reliable in text processing than Emacs

“Renewal of large-scale website URL”

  • Presentation at PHP Conference Japan 2017
  • We had to normalize many class and function names

phpcon2017.png

Why Vim script?

  • Vim features are more powerful than AWK and sed
    • Text object, macro, register, auto-indent, …etc.

why-vim-script.png

Rename class name using Vim script

  • This example is simple, so it can be done with shell script and sed
  • A complex refactoring of hundreds of files was actually handled by Vim script

rename-class.png

Prepare for text processing in Vim

Read “Practical Vim” book (ja:実践Vim)

practical-vim.png

Read “Practical Vim” book (ja:実践Vim)

I read this book several times but I don't remember it almost because I have a weak memory…

practical-vim.png

Execute Vim script from shell

  • Create a shell script
#!/bin/bash

vim -N -u NONE -i NONE -e -s -S "$0.vim" "$@"
  • The combination of -e and -s means start in ex mode and silent mode
  • Omitting that option during development will make debugging easier

Write vim script

  • TIP 100: Alphabetize the Properties of Each Rule in a CSS File
global /{/ .+1, /}/-1 sort

" デバッグ中はここ
update  " 変更点があるファイルにすべて書き込む
qall!   " すべての終了

Write vim script

  • Implement tac command with Vim script
let i = 1
let lines = line('$')
while i < lines
    :$
    normal! dd
    execute ':' . i
    normal! P
    let i = i + 1
endwhile

update
qall!

Conclusion

Conclusion

  • Vim replaces text processing with sed and awk
  • Describe your Vim operation in script and enable efficient text processing for multiple files
  • I'm immature and still can't write complicated processes, but you can probably use headless Vim
  • I have experience with Emacs buffer operations using Lisp, so writing a Vim script for text processing was a fun experience like a puzzle.

Thank you! ヾ(〃><)ノ゙☆


Reference document