Commit Messages in GNU Nano

When commiting any changes using Git, it is important to include relevant and well constructed commit messages for other developers – as well as your future self – who may be involved in the project. A decently crafted commit message can help speed up code comprehension, hopefully allowing others to quickly grasp what problem a commit is addressing and how it is going about solving it.

In this article, we will develop a workflow that utilises the GNU Nano editor – a terminal-based text editor that ships with several Linux distros – to format commit messages, so that they comply with the Tim Pope's 50/72 principle. For insight on why we should bother abiding to this principle, see Chris Beam's article on how to write git commit messages.

Modifying the nanorc file

We will start off by configuring GNU Nano to wrap lines of text after 72 characters.

First, we will navigate to the /etc directory.

$ cd /etc

Now that we are in the directory containing the nanorc configuration file, we will open this file using the GNU Nano editor. Run the following command:

$ sudo nano nanorc

The above command will open the nanorc file in the terminal. We can move the cursor up and down using the arrow keys on the keyboard. We can also make use of some GNU Nano shortcuts to easily and comfortably edit the file.

There are 2 changes we need to make to the nanorc file. The first change enables line wrapping. The second one ensures that wrapping occurs at or before the 72nd character of a line.

  1. set nowrap# set nowrap

    We comment out nowrap to disable its effect.

  2. set fill -8set fill 72

    For the curious, the default value of -8 means that lines will wrap at 8 characters less than the width of the terminal. So if the terminal were to be sized at 100 characters/columns wide, then lines would wrap at the 92nd character mark.

To save the changes we have made, press Ctrl + O and to overwrite the file press Enter. The file will remain open in the editor, so to close the GNU Nano editor press Ctrl + X.

Writing commit messages with Nano

To verify that GNU Nano is the default editor in our terminal, use the command below and if necessary, set Nano as the default.

# This command lists the available editors, and allows
# us to select one as the default
$ sudo update-alternatives --config editor

Assuming that in our local repository, there are some changes that have been staged for commit, we can run this command:

$ git commit

This will open up the COMMIT_EDITMSG file using GNU Nano. Git uses this file to store the commit message that corresponds to a particular commit.

Following the 50/72 principle, we will begin by typing out a subject line – ideally one that is at most 50 characters long. If the change we have made is small and does not need to be described further, we can save the file in the same way we saved our changes to the nanorc file.

However, if we want to provide more details about the changes introduced in our commit, we should type out a more detailed description in the body of our commit message. Remember to include a blank line between the subject and body.

Due to the changes we made to the nanorc file, the GNU Nano editor will automatically wrap text at 72 characters.

GNU Nano shortcuts

The bottom tab of the GNU Nano editor displays several shortcuts such as ^X Exit. This means that to close the editor we should press Ctrl + X. However, there can be scenarios where the same keybinding for a particlar shortcut in GNU Nano is the same one used for a shortcut in another program that may also be running – for instance, if we are using a code editor with an integrated terminal, some shortcuts may affect both.

A prime example of this would be while using the Cloud9 IDE to develop in our web browser. The ^W Where Is shortcut will present a few problems. Typically, pressing Ctrl + W within a web browser will cause the current tab to close. Even if we disable this particular web browser shortcut or re-map it to a different keybinding, Cloud9 defaults to using Ctrl + W to close a pane – a small window within the Cloud9 IDE interface that contains tabs, of which our terminal would be one.

To ciricumvent this issue, we can press the Esc key twice and then the key that appears after the ^. For instance, to make use of the ^W Where Is shortcut, we would use the following key sequence, pressing the keys one after the other:
EscEscW

The ^W Where Is shortcut is used to search for strings. It is useful if you know what you are looking for within a file and are not too inclined to scroll and search for it yourself – case in point: finding the lines in the nanorc file that need to be changed.