Today I Learned
Return to previous file after 'g'+'f'
Posted on March 7, 2018

If you want to jump to a file in vim it’s pretty easy to get there by placing your cursor over the file you want to teleport to and then hit the g+f keys.

What I didn’t know, is that you can easily go back to your previous file pressing CTRL+O.

Simple, yet so productive.

How to use an index on a Collection's Partial
Posted on February 21, 2018

When iterating through a collection in a rails view, I typically use a block to do so right in my view.

If I need different id’s in the DOM for each element, it’s quickly achievable with the with_index method which provides the index of each item as it iterates.

<!-- index.html.erb -->
<ul>
  <% @products.each.with_index do |product, index| %>
    <li id="product<%= index %>"><%= product.name %></li>
  <% end %>
</ul>

While with_index works when you’re iterating through a collection within your view, you but how would you create different id’s in the DOM if you’re rendering your collection within a partial?

According to Local variables in Rails documentation, when you’re rendering a collection with a Rails partial, you can use a counter as you iterate over that collection.

That same example rendering the collection with a rails partial would look like.

<!-- index.html.erb -->
<ul>
  <% render partial: "product", collection: @products %>
</ul>

<!-- _product.html.erb -->
<li id="product<%= product_counter %>"><%= product.name %></li>
Skip test-unit on `rails new`
Posted on February 1, 2018

While starting a new rails app, I noticed that I could simply skip installing Test::Unit from the start. This left the initial application ready for Rspec to be installed and configured.

rails new my_app --skip-test

Pretty simple. Kicking myself for removing it manually each time I decided to use Rspec.

How to minify a css or js file in vim
Posted on January 26, 2018

I wanted a quick and easy way to minify a CSS file for this site, without adding any fancy build tools like webpack, using a quick command.

The ideal workflow was:

  • Open a regular CSS file.
  • Minify the current file.
  • Save over the file.

After some digging and a node library, I came up with this.

  1. Install Minify via NPM
    npm install -g minify
    
  2. Open css file in vim
    vim style.css
    
  3. Now that you are in vim, create a new line at the top of the file using Shift + O
  4. Read in the existing file, parse it through minify and paste it in place.
    :r !minify %
    
  5. Move down a line to the uncompressed css and delete the rest of the file using d + G

If you are looking to minify lots of files for an app there are clearly better options, but if you just want to shrink the size of a single file on a static site this is my new favorite.

Running rails dev server in the background
Posted on January 16, 2018

When I am working on UI in a rails app, I typically don’t care to read the information in a rails debugger.

Running rails in the background is as easy as:

rails s -d

Quitting the process is slightly tricker than the good ol fashion cmd-c

kill -9 $(cat tmp/pids/server.pid)
How to Refresh CtrlP Cache
Posted on January 4, 2018

I really enjoy using ctrlp.vim for fuzzy finding files in vim. Makes navigation really easy.

By default, ctrlp caches your directories upon first use for faster finding later on. That typically works great until you add or delete files, and your cache is no longer valid.

Fortunately, you can quickly purge your cache in ctrlp by clicking F5 once ctrlp is open.

I must have missed this line in the documentation.

How to search and replace within a range in vim
Posted on January 3, 2018

A quick “search and replace in vim” google search will likely result in vim command looking something like this:

%s/foo/bar/g

While useful, you may have a big file and want to restrict where you find and replace. The % operator can be omitted to search a single line, or be replaced with a range of lines, like so:

" Singe line
s/foo/bar/g 

" Range
1,5s/foo/bar/g
Blogging with Jekyll in Vim
Posted on November 26, 2017

I always wanted to love Jekyll as a blogging platform.

Unfortunately, creating awkwardly named markdown files and copying and pasting necissary front matter to the top of every text document was a shittier experience than simply clicking a “New post” button on some blogging CMS like WordPress or Ghost.