Configuration for a cleaner command history in ZSH

Being able to scroll back through all of the commands entered into the terminal with the up arrow key is very handy. A level of annoyance is added when having to scroll back through all the occurrences of a command that was repeated multiple times in succession however.

Fortunately, there’s an option you can set in your .zshrc to reduce a block of repeated commands down to just one line in the command history.

setopt hist_ignore_dups # Don't save multiple instances of a command when run in succession

You can go further with the setopt hist_ignore_all_dups option. With that set, all previously saved commands that match the command you just entered will be removed from the history. I personally don’t like this though, as when I’m going back through my history I like having the complete context around any previously entered command.

Lastly, in the name of a cleaner command history, you can set setopt hist_reduce_blanks which will just remove any superfluous whitespace from the command before saving it to the history.

Conditionals and regular expressions in Javascript

Regular expressions (regexes) are, at first, baffling to look at. What they lack in initial digestibility however, they make up for in power.

Before going any further with this post, let’s take a quick look at the structure of a regular expression in case you haven’t used one before.

Read on, there’s more »

Section comments with centred text in Vim

Everyone has their own taste when it comes to comment styles. I personally like section comments with centred text as shown below. They’re time consuming to get right though so I tend to opt for a simpler format.

/* -------------------------------------------------------------------------- *
 *                                 COMPONENTS                                 *
 * -------------------------------------------------------------------------- */

Some light procrastination led me to investigating how much of the above layout could be automated in Vim. As it turns out, all of it. Here’s what the final workflow looks like in action.

The mapping isn’t particularly pleasant to read, but it works.

nnoremap <leader>sh VU:center 79<cr>0c2l<space>*<space><esc>A<space><esc>40A<space><esc>d79<bar>ch<space>*<esc>YPS<esc>i/*<space><esc>74A-<esc>A<space>*<esc>jo<esc>S<esc>i<space>*<space><esc>74A-<esc>A<space>*/<esc>o<esc>o<bs>

As a reminder, you can change the mapped commands based on the filetype of the file being edited. That way, <leader>sh can always be “section header” but the comment syntax can be changed for languages that don’t use /* */.

autocmd BufRead,BufNewFile *.{es6,js,scss,css} nnoremap <leader>sh VU:center 79<cr>…

jQuery to pure JS: Target each direct child of an element

If you’re coming from jQuery to pure Javascript and are trying to target each direct child of an element, your first attempt to select the direct children will probably be element.querySelector('> *'). That will throw an error though, because .querySelector() doesn’t support tag-name wildcards.

Your next attempt might involve element.childNodes, and you’d be closer. The issue with that is that .childNodes will return all of the element’s children, not just the direct children.

Read on, there’s more »

Jekyll: Create a list of all posts in the same category

Let’s take a look at how to create a list of all of the posts in the same category as the one currently being viewed, bearing in mind three requirements:

  1. If the post has more than one category, we want to create a list for each category.
  2. We don’t want to show the list if there is only one post in the category (the one being viewed).
  3. We don’t want to include the post who’s page is currently being viewed within the list.

Read on, there’s more »

Jekyll: Display the full post unless a custom excerpt is specified

The homepage of this site lists the full content of each post by default. An excerpt is shown only if a post contains <!--more-->, which is the default excerpt separator in Jekyll.

Describing this functionality in prose looks something like this. For each post that is looped through, check it’s content for the excerpt separator. If found, then show all the content up until the separator, then a read more link. If not, show the full content of the post.

Translating the above to liquid results in the following snippet within the posts loop.

{% if post.content contains site.excerpt_separator %}
  {{ post.excerpt }}
  <a href="{{ post.url }}">Read more</a>
{% else %}
  {{ post.content }}
{% endif %}

If <!--more--> isn’t to your liking, excerpt_separator can be configured globally in _config.yml.

Mummy’s Boy

Winko

On Monday afternoon—in his favourite spot in the world; by the fire in the sitting room—we said goodbye to our friend, companion, source of laughter and shoulder to cry on of the past 12 years: Winko.

Read on, there’s more »

Guard clauses in Javascript

I first came across the idea of guard clauses in Ruby. The concept is a fairly straightforward one: instead of wrapping a block in an if statement, you exit on a single line, keeping the proceeding code at the same indentation level.

In a language like Javascript, there’s no end-benefit to this style of exiting. There is, in my opinion, a benefit for anyone touching your codebase however: it improves readability and just feels nicer; cleaner.

An example then. Traditionally, you might stop certain code running like this.

const assertString = (str) => {
  if (typeof str === 'string') {
    return true
  } else {
    return false
  }
}

Using a guard clause, you would write the above like so.

const assertString = (str) => {
  if (typeof str !== 'string') { return false }
  return true
}

I’ve adopted this style of exiting everywhere applicable and really like it. I’d encourage you to give it a whirl.

Now serving over HTTPS and HTTP2

Every so often I check to see if GitHub have added a way to serve websites hosted on GitHub Pages with a custom domain over HTTPS rather than HTTP. Whilst clicking around to that end last night, I found that the answer is still no. I did stumble upon a service called Netlify however.

As this site uses Jekyll plugins I can’t push directly to my username.github.io repo, it needs to be built first. To avoid having to do that manually, I setup a whole song and dance on Wercker that turned out to be more hassle than it was worth.

Netlify is, in short, a one-stop-shop for static sites. The features that attracted me to it were the Git integration—all I need to do is push a post to the repo and it’ll handle the rest (including building the site despite it using plugins, taking Wercker out of the mix), the free SSL certificates via Let’s Encrypt and the HTTP2 support. A global CDN, and fast DNS aren’t features to shake a stick at either, but GitHub pages has those covered too.

The setup was quick and easy: I pointed Netlify at the site’s repo, it detected that the generation engine was Jekyll, automatically setup a box, built it and served a preview. From there, I just removed the two A records that pointed at GitHub and added one that points to Netlify, added my custom domain in the admin and waited for the lot to propagate. Once it had, SSL was a click away and only one more click was needed to force TLS connections.

If your site’s repo is public on GitHub then Netlify gives you all this functionality on the house. From the few hours I’ve spent with it, I’d highly recommend it as an alternative to GitHub Pages.