I, like most developers, am lazy (which is apparently a virtue!) and recently needed to remove a bunch of
CREATE TABLE statements from a mysqldump. The vim macro below is how I accomplished that task.
qx # Begin recording the command in to the register x /CREATE TABLE # Search for the next instance of CREATE TABLE j # j down to the next line; this places us inside the parentheses of the statement di( # delete inside parentheses. This works on single and multi-line values nicely dk # Delete the closing parenthesis line, but also the line above (CREATE TABLE) q # Command is finished, stop recording
Now you can run your macro once by typing
@x in command mode or multiple times using something like
CREATE TABLE isn't found, the rest of the macro stops, and you'll get the typical message
Pattern not found: CREATE TABLE. This macro will stick around until you close vim, at which point you'll have to record it again. It is possible to save a macro permanently, but that is left as an exercise to the reader.
Do you have any awesome vim macros that have saved you time and/or keystrokes? I'd love to hear about them!