A handy code cleanup workflow

Drag to Evernote

If you’re a developer like me, you want your code to be clean, DRY, and easy to maintain. In order to do this, we often have to purge files of function calls, add classes to markup, or remove superfluous or deprecated code. I’ve recently developed a simple workflow that makes these tasks quite simple and quick, leaving me with code that is easier to read, understand, and maintain.

Requirements

For this workflow I’m going to use ack, Vim, Finder, and Evernote.

A few simple steps

First, you have to find all of the places in your code where you need to make the edits. I use ack a dozen times a day, often before lunch. For this example I’m going to search for all calls to the function get_header and pipe the results to my editor, Vim. You could also use grep.

ack get_header | vim -

Second, use Vim to clean up the results before saving:

wp-content/themes/brentdanley/index.php:8:<?php get_header(); ?>
wp-content/themes/brentdanley/page.php:1:<?php get_header(); ?>
wp-content/themes/brentdanley/search.php:1:<?php get_header(); ?>
wp-content/themes/brentdanley/single.php:1:<?php get_header(); ?>

To get just a list of files, I use the substitution command to remove everything from the first colon to the end of the line, then sort to remove duplicates.

:%s/:.*//
:sort u

Which gives me this:

wp-content/themes/brentdanley/index.php
wp-content/themes/brentdanley/page.php
wp-content/themes/brentdanley/search.php
wp-content/themes/brentdanley/single.php

Not bad so far, right?

We temporarily save the file to the Desktop and open it up in Finder.

:w ~/Desktop/get_header_files.txt

Third, drag the temporary file in Finder up to the Evernote app icon and drop it. If you don’t have the Evernote app icon in your Finder toolbar, I highly recommend you add it now.

Drag to Evernote
Drag to Evernote

This will automatically add your list of files to Evernote as a new note and open it for your convenience, which brings us to the final step.

Simply select the contents of the body of the note (⌘-a) and click the checkbox button.

Create the checklist in Evernote
Create the checklist in Evernote

Voila! You now have a convenient checklist of all the files that require your attention.

Alfred alternative

In lieu of saving the file in Vim, then dragging it to the Evernote icon in the Finder toolbar, you could select and copy the list in Vim after you’ve cleaned it up, then use Alfred to create a new note from the clipboard. I doubt this would save much time, but it does eliminate the need to create an intermediate file and open Finder.

Alfred: Create a new note from clipboard
Alfred: Create a new note from clipboard

If you have a better workflow or tips that would improve mine, please leave a comment. Thank you.

Leave a Reply