<p>I had a ridiculous typo in a Git commit message recently, so I decided to explore spell checking in Vim. As it turns out, it’s extremely easy.</p> <p>I only wanted it enabled for Git commit messages and markdown, so I added the following to my vimrc / init.vim:</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" Spell-check Markdown files and Git Commit Messages</span> autocmd <span class="nb">FileType</span> markdown <span class="k">setlocal</span> <span class="nb">spell</span> autocmd <span class="nb">FileType</span> gitcommit <span class="k">setlocal</span> <span class="nb">spell</span> </code></pre></div> <p>By doing this, it highlights potential misspellings in red underline. You can see a list of potential corrections by moving the cursor over the word in normal mode and...</p>
Blog Entries tagged "til"
<p>Here’s a handy Vim command that I find myself searching for often. It could easily be made into a function in your vimrc.</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" 4 spaces to 2 spaces</span> %s;^\<span class="p">(</span>\s\<span class="p">+</span>\<span class="p">)</span>;\<span class="p">=</span><span class="nb">repeat</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nb">submatch</span><span class="p">(</span><span class="m">0</span><span class="p">))</span>/<span class="m">2</span><span class="p">)</span>;<span class="k">g</span> <span class="c">" Tab to 2 spaces</span> %s<span class="sr">/\t/</span> /<span class="k">g</span> </code></pre></div>
<p>When using Elixir, I’ve long missed the special <code>_</code> helper from <a href="https://en.wikipedia.org/wiki/Interactive_Ruby_Shell">irb</a> that returns the result of the previous expression. Sometimes I actually type it out of pure habit.</p> <p>Little did I know that Elixir has the same thing, except better.</p> <p>IEx.Helpers has a function <code>v(n \\ -1)</code> that returns the value of the nth expression in the session history. So <code>v</code> alone returns the previous expression and <code>v(-2)</code> returns the one before it.</p> <div class="highlight"><pre class="highlight elixir"><code><span class="n">iex</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">></span> <span class="s2">"abc"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">></span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">></span> <span class="n">v</span> <span class="s2">"zyx"</span> <span class="n">iex</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span><span class="o">></span> <span class="n">v</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="s2">"abc"</span> </code></pre></div>
<p>Ruby has some handy Array shorthands, but I always forget which is which, so I thought I should write them down.</p> <h3 id="symbol-array-shorthand">Symbol Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%i{foo bar}</span> <span class="c1"># => [:foo, :bar]</span> </code></pre></div> <h3 id="string-array-shorthand">String Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%w{foo bar}</span> <span class="c1"># => ["foo", "bar"]</span> </code></pre></div> <p>Both also work with square brackets <code>%w[a b c]</code> or parenthesis <code>%i(a b c)</code></p>
<p>Today I needed to access a production database for my Rails app directly using a GUI application on my mac, so I figured out that this can be done by creating an SSH tunnel like so:</p> <div class="highlight"><pre class="highlight shell"><code>ssh <span class="nt">-Ng</span> <span class="nt">-L</span> <local-port>:<remote-host>:<remote-port> <user>@<remote-host> ssh <span class="nt">-Ng</span> <span class="nt">-L</span> 3307:100.64.26.11:3307 adam@100.64.26.11 </code></pre></div> <p>And then, in your Rails app, update the database.yml as if you were connecting to a local database, but specify the proper database name.</p> <div class="highlight"><pre class="highlight yaml"><code><span class="na">development</span><span class="pi">:</span> <span class="na">adapter</span><span class="pi">:</span> <span class="s">mysql2</span> <span class="na">database</span><span class="pi">:</span> <span class="s">myapp_production</span> <span class="na">host</span><span class="pi">:</span> <span class="s">127.0.0.1</span> <span class="na">port</span><span class="pi">:</span> <span class="m">3307</span> <span class="na">username</span><span class="pi">:</span> <span class="s">root</span> <span class="na">password</span></code></pre></div>
<p>I use Rails’ <code>present?</code> method constantly, but recently I stumbled across the <code>presence</code> method.</p> <p>In the past, I have often found myself doing the following because params[:foo] could be nil or a blank string.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">foo</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">].</span><span class="nf">present?</span> <span class="p">?</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">]</span> <span class="p">:</span> <span class="s2">"something else"</span> </code></pre></div> <p>I hate that i have to write <code>params[:foo]</code> twice. But the <code>presence</code> method is handy for this case. It returns nil if the item is nil or a blank string, but otherwise returns the value.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">foo</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">].</span><span class="nf">presence</span> <span class="o">||</span> <span class="s2">"something-else"</span> </code></pre></div>
<p>I’m surprised I didn’t already know this, but if you have an ActiveRecord <code>where</code> statement that references an id column, you can just pass in an object and it will pass in the object’s id, not the string representation of the object.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">company</span> <span class="o">=</span> <span class="no">Company</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="s2">"users.company_id = ?"</span><span class="p">,</span> <span class="n">company</span><span class="p">)</span> <span class="c1"># is equal to</span> <span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="s2">"users.company_id = ?"</span><span class="p">,</span> <span class="n">company</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span> </code></pre></div> <p>So when you create named scopes, you don’t have to do the song-and-dance where you check to see if the object is a model or an integer, as shown below.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">scope</span> <span class="ss">:by_company</span><span class="p">,</span> <span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="n">company</span><span class="o">|</span> ...</code></pre></div>
<p>With <a href="https://ifconfig.co">ifconfig.co</a>, you can lookup your external IP Address and Location Information using a simple curl command. You can use it like so:</p> <div class="highlight"><pre class="highlight shell"><code><span class="nv">$ </span>curl ifconfig.co/json <span class="o">{</span> <span class="s2">"ip"</span>: <span class="s2">"96.27.202.202"</span>, <span class="s2">"ip_decimal"</span>: 1612409628, <span class="s2">"country"</span>: <span class="s2">"United States"</span>, <span class="s2">"country_iso"</span>: <span class="s2">"US"</span>, <span class="s2">"city"</span>: <span class="s2">"Columbus"</span>, <span class="s2">"hostname"</span>: <span class="s2">"d27-96-101-101.nap.wideopenwest.com"</span> <span class="o">}</span> <span class="nv">$ </span>curl ifconfig.co/city Columbus <span class="nv">$ </span>curl ifconfig.co/country United States </code></pre></div>