Removing Leading Zero in Date Format

The date command allows us to display the current date and time.

$ date
Sun May 26 01:16:25 +08 2019

Most importantly, we can format the date and time using specifications from strftime. Now, assume we want a format like 26/5.

$ date '+%d/%m'
26/05

Notice that %m yielded 05 instead of 5. This is because the values for %m are 01 - 30. So, to remove the leading zero, we need to do %-m. The same goes for %-d.

$ date '+%-d/%-m'
26/5

Adding CSS class to Phoenix `form_for`

Passing in class: "form-class" to form_for in Phoenix doesn’t work. Instead, it expect a keyword list. Hence, to add custom CSS class to form_for, use:

<%= form_for @conn, ... , [class: "form-class"], fn f -> %>
   ...
<% end %>

All of the HTML attributes of a form element can be pass in this way. For more available options, see Phoenix.HTML.Form documentation.

Editing Long Commands in Shell

When typing long commands in the shell, you can do ctrl-x-e to invoke your editor based on the environment variable $EDITOR. If you use Vim, edit the command, and do :wq. This will bring you back to the shell with the edited command.

Tags: shell

Non-Greedy Matching in Vim

Suppose we have the following text in Vim:

<span>Hello World</span>

Using /<.*> to search would match not only both <span> and </span> tags, but the words inside them as well, because .* is greedy. To only match the tags alone, we need to use /<.\{-}>. See :help non-greedy for the documentation on .\{-}.

Tags: vim

Logs for One Branch against Another

To view the logs for a specific branch in respect to another branch, we can do git log develop..feature. This will show us only the new commits on feature if it was branched off from develop.

Alternatively, if you are already on the feature branch, you can do git log develop..@ or just git log develop...

Tags: git