chunkhang profile picture

chunkhang

Tea over coffee

Visit Website

Git Patches

You may be familiar with git stash, but patches can also be very useful. Assuming you have changes to the working directory, instead of stashing it, we can create a patch:

git diff > example.patch

With that, we have created a patch file that contains the current diff. Now, instead of stashing, we can just discard all changes. To apply the patch, just do:

git apply example.patch

This is similar to running git stash pop to update the current working directory with the diff from example.patch. With patches, it’s easier to keep track of what each patch does. On top of that, you can distribute patches to other people so they can quickly apply the change to their repository. It’s super useful to create patches for testing a feature for example. Your reviewer can then just apply the patch when they’re reviewing your branch.

To revert the patch, simply do:

git apply -R example.path

You can use either -R or --reverse.

Tags: git

Formatting JSON in Vim

You probably know that we can use something like :%!python -m json.tool to format a JSON file that’s opened in Vim. However, I always find this command hard to recall. Instead of dedicating a mapping for this, we can actually rely on equalprg instead. Let’s set this autocmd in .vimrc:

augroup vimrc
  autocmd!
  autocmd FileType json setlocal equalprg=python\ -m\ json.tool
augroup END

Now, when we open a JSON file, we can use the familiar = key to format it nicely.

Verbose Git Commit

After staging your files, doing git commit will launch your editor, typically Vim, for you to enter the commit message.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#   modified:   vimrc
#

However, if you do git commit -v, you will get to see the diff for the commit you are about to make as well. This is a useful reminder for when you are writing the commit message.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#   modified:   vimrc
#
# ------------------------ >8 ------------------------
# Do not touch the line above.
# Everything below will be removed.
diff --git a/vimrc b/vimrc
index cc63644..fb3832c 100644
--- a/vimrc
+++ b/vimrc
@@ -439,6 +439,11 @@ nmap <C-f>f <Plug>CtrlSFPrompt
 nmap <C-f>w <Plug>CtrlSFCwordPath
 nnoremap <C-f>t :CtrlSFToggle<cr>
 
+" -----------------------------------------------------------------------------
+" emmet.snippets
+" -----------------------------------------------------------------------------
+imap <C-e>, <esc>dawae<tab><esc>pa<tab>
+
 " -----------------------------------------------------------------------------
 " ultisnips
 " -----------------------------------------------------------------------------

To make this the default behavior of git commit, you can do git config --global commit.verbose true. This will add the following entry into .gitconfig.

[commit]
  verbose = true

Tags: git

More from Less

The less command actually accepts a lot of configuration options. For instance, less -F <file> will make the pager quit if the file can be displayed within one screen. We can also do export LESS='-F' so specific options apply to all less commands. That way, other commands that use less like git and man will also benefit from the environment variable. Use man less to read more about all the different kind of options you can use to make less more. Here is a nice article that covers some interesting options.

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