Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Thursday, February 18, 2016

Big list of files to edit? vim to the rescue (again)

Did you know that you can treat the text under the cursor as a filename, and open that up for editing right in vim? Here's how:

  • gf will open the filename under the cursor in the current window
  • ^Wf will open it in a split window
  • ^Wgf will open it in a new window

Thursday, January 28, 2016

Vim gem: built-in calculation

Vim is my go-to editor. Has been for 20 years. Besides being an all-around awesome editor for composing text, it also has some handy built-ins, like calculations:

  • In insert mode, ^R= accepts a mathematical expression, the result of which will be inserted in place.
  • In normal mode, ^A increments the number at (or to the right of) the cursor by one, while ^X decrements it by one. These accept repeats, so 5^A will add 5 to the number.

Thursday, January 21, 2016

Using vim to replace string functions with their multi-byte equivalent

The PHP INI option mbstring.func_overload override certain string functions (like strpos, substr, etc.) with multi-byte aware implementations. This makes it super easy to migrate a legacy code base to UTF-8, but immediately restricts interaction with vendor products (Symfony, Net_DNS2, etc.).

The proper integration is to manually replace string functions with their multi-byte equivalent. In one legacy code base I'm improving, there are on the order of 10k instances of these functions. I want to change and verify each replacement, but I don't want to type much. Time for some vim-fu:
:argdo %s/strpos/mb_strpos/gc | wn
This performs a confirmed find and replace, writes the change to disk, then moves on to the next file.