Vim, not Vi, on the command line
“Vi mode” is well known about. In bash or zsh, it’s possible to move around much in the same way you would in vi (e.g. b goes back a word). What isn’t as well known, is that zsh, 5.0.8 or later, offers support for visual mode and text objects. For anyone who hasn’t yet seen the vim light, this might not mean much but read on…
Say I have a command to create a new git branch but upon copy/paste I notice an error:
git checkout -b CREATURE-my-new-feature
I’m an idiot. That should say FEATURE as per my team’s branching convention. Ok, so in emacs or vi mode I can move back to the “C” in “CREATURE” easily enough using M-b or b respectively but then what? zsh’s Vim text objects allow me to make the corrections in a vim like manner:
- ciwFEATURE rewrites CREATURE as FEATURE.
- Even better, ctEF changes to E with an F. This makes the same amendment in a more succinct way.
- If I wanted to rename the whole branch with something completely different then I could ciWnew-name. (Notice the capital W to change the major text object).
If bash is your shell of choice, then I still recommend trying vi mode for a while, even if it isn’t as fully featured as zsh’s version:
bash setting: set -o vi
zsh: bindkey -v
WTF is a text object
As usual, I’m jumping straight to the point. If you haven’t stumbled across text objects in Vim yet then I recommend opening it up and trying the command:
Alternatively, there’s a great post here that explains text objects in detail!