2010-03-24

Vim: toggling between .h / -inl.h / .cc / .py / .js / _test.* / _unittest.*

It's often that you need to toggle between x.h, x.cc, x_test.cc, x_unittest.cc, or x-inl.h.
Here's script to do this:
"switch between .h / -inl.h / .cc / .py / .js / _test.* / _unittest.*
"with ,h ,i ,c ,p ,j ,t ,u
"(portion from old mail from David Reiss)
let pattern = '\(\(_\(unit\)\?test\)\?\.\(cc\|js\|py\)\|\(-inl\)\?\.h\)$'
nmap ,c :e =substitute(expand("%"), pattern, ".cc", "")
nmap ,h :e =substitute(expand("%"), pattern, ".h", "")
nmap ,i :e =substitute(expand("%"), pattern, "-inl.h", "")
nmap ,t :e =substitute(expand("%"), pattern, "_test.", "") . substitute(expand("%:e"), "h", "cc", "")
nmap ,u :e =substitute(expand("%"), pattern, "_unittest.", "") . substitute(expand("%:e"), "h", "cc", "")
nmap ,p :e =substitute(expand("%"), pattern, ".py", "")
nmap ,j :e =substitute(expand("%"), pattern, ".js", "")


,h: X.cc or X-inl.h or X_test.cc or X_unittest.cc goes to X.h
,c: X.h or X-inl.h or X_test.cc or X_unittest.cc goes to X.cc
,i: X.h or X.cc or X_test.cc or X_unittest.cc goes to X-inl.h
,p: X_test.py or X_unittest.py goes to X.py
,j: X_test.js or X_unittest.js goes to X.js
,t: X.h or X-inl.h or X.cc or X_unittest.cc goes to X_test.cc
,t: X.py/js or X_unittest.py/js goes to X_test.py/js
,u: X.h or X-inl.h or X.cc or X_test.cc goes to X_unittest.cc
,u: X.py/js or X_test.py/js goes to X_unittest.py/js


Obviously you can customize different key mappings to do the toggle. =)
Post a Comment