Category Archives: macOS/*BSD

macOS/OS X, FreeBSD/OpenBSD/NetBSD

Enable Natural Scrolling for Trackpads Using libinput

Libinput is a library to handle input devices in Wayland and X.Org. It can be used as a drop-in replacement for evdev and synaptics in X.Org, and it is supported by a wide range of desktop environments, including GNOME and Xfce. In this post, we will see how to enable natural scrolling for trackpads using libinput. We will also leave mouses alone, i.e., no natural scrolling for mouses.

First, we need to know the name of the trackpad to enable natural scrolling for. This can be easily known by executing xinput --list. My output includes the following:

⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ bcm5974                                   id=13   [slave  pointer  (2)]

It is easy to see that my trackpad is bcm5974.

Continue reading

Parallelize make by Default

The make utility is an standard utility on POSIX systems (GNU/Linux, macOS, etc.) that update files derived from other files, such as compiling source files to their binary forms. It is widely supported and used across different fields such as organizing and building C/C++/Fortran projects, building Sphinx documentation, etc.

The most popular implementation of the make utility is probably GNU make, which is usually the default make program on various GNU/Linux distributions. (On macOS, the version of the default GNU make is pretty old. Please consult Install and Use GNU Command Line Tools on macOS/OS X for a newer version.) It adds one very important feature besides the standard make specification: parallelization. The command line option -j can be used to specify the maximum number of jobs that it is allowed to run simultaneously. However, it is quite annoying to type up this option every time when using it—we want a setting such that the CPU can be fully utilized by default. To achieve this goal, add the following lines to your ~/.bashrc if you use bash or ~/.zshrc if you use zsh:

if type nproc &>/dev/null; then   # GNU/Linux
  export MAKEFLAGS="$MAKEFLAGS -j$(($(nproc)-1))"
elif type sysctl -n hw.ncpu &>/dev/null; then   # macOS, FreeBSD
  export MAKEFLAGS="$MAKEFLAGS -j$(($(sysctl -n hw.ncpu)-1))"

The code above sets the envrionmental variable MAKEFLAGS, which specifies the command line arguments of any invoked make subprocesses. It is set such that the maximum number of jobs that is allowed to be run simultaneously is equal to the number of available CPU cores minus 1. In this way, the hardware is more or less fully utilized when using make, with one CPU core left for other potential tasks on the system.

A Better ls Command

The ls command is a command to list files on a UNIX-like system. It is probably one of the most used command. However, a plain ls command without any polishing may look really “plain”. Here, we will slightly configure this command to make it more usable:

  • More colorful output
  • Automatic pagination for long file lists
  • File type indication
  • Human readable sizes
  • Natural ordering of files


Output of ls before configuration

Output of ls before configuration

Output of ls after configuration

Output of ls after configuration

ls pagination

Pagination for ls

Continue reading

Speed Test: Check the Existence of a Command in Bash and Zsh

In both bash and zsh, there are multiple methods to check whether a command exists. In this post, a set of speed tests will be performed on them to find the fastest way in each of the two shells (NOT to compare the two shells). We will test 5 different methods (foobar is the command to test for existence in the list):

  • type foobar &> /dev/null
  • hash foobar &> /dev/null
  • command -v foobar &> /dev/null
  • which foobar &> /dev/null
  • (( $+commands[foobar] )) (zsh only)

All the methods listed above will have a return status of zero if the command foobar exists, otherwise non-zero. That is, after replacing testing-command by any of the commands listed above, you can test the existence of the command foobar by executing testing-command && echo exist || echo non-exist.

Throughout this post, ls will be the command that is used for testing existence, which does exist on the system which runs the tests. The test environment is Debian Jessie with bash 4.3.30 and zsh 5.0.7 on Intel Xeon processor E3-1240 v3 (8 MB Cache, 3.4 GHz). The test scripts are also available at the end of the post.

Continue reading

Restore the Previously Canceled Command in Zsh

In zsh, it is often annoying that we can’t easily restore the command we just canceled by CtrlC: canceled commands are not recorded into the history file and thus cannot be restored by searching previous commands. To make the restoration possible, zsh provides a variable ZLE_LINE_ABORTED which keeps a record of the last command that was canceled—everything looks so simple. However, for some reasons, such as canceling stuck tab completion, we often push CtrlC for more than once—but ZLE_LINE_ABORTED would become empty if CtrlC is used on an empty line! Thanks to the great extensibility of zsh, we can solve this issue by tweaking zle-line-init (add the following to your ~/.zshrc):

function zle-line-init {
  # Your other zle-line-init configuration ...

  # Store the last non-empty aborted line in MY_LINE_ABORTED
  if [[ -n $ZLE_LINE_ABORTED ]]; then

  # Restore aborted line on the first undo.
  if [[ -n $MY_LINE_ABORTED ]]; then
    local savebuf="$BUFFER" savecur="$CURSOR"
    zle split-undo
    BUFFER="$savebuf" CURSOR="$savecur"
zle -N zle-line-init
  • Line 1: Define the zle-line-init widget which will be executed every time when the a new command line is ready to take input.
  • Line 5-7: If ZLE_LINE_ABORTED is non-empty, save it to MY_LINE_ABORTED.
  • Line 10-16: If MY_LINE_ABORTED is non-empty, the initial undo will restore the contents in MY_LINE_ABORTED. Also see man zshzle for an explanation of split-undo.
  • Line 18: Install the widget zle-line-init.

Now type any command and push CtrlC twice and undo (bound to Ctrl/ by default): your canceled command is back!

Note that if you use zsh-autosuggestions this code snippet somehow breaks it. Adding _zsh_autosuggest_widget_clear before the end of zle-line-init would fix it.


Make the less Command More Powerful

Due to its speed and simplicity, GNU less is probably the most common default terminal pager on various GNU/Linux distributions—you may have probably used it explicitly via the less command, or implicitly when you execute the man command or git diff. Although the default configuration of less does not really offer much except for a basic text viewer, it is actually much more powerful than most people think. Here a few improvements over the default configuration are offered.

For macOS/OS X users: consider installing a newer version of less and other GNU command line utilities. To do so, you can follow the instructions here.

Continue reading

Back up (Migrate) Homebrew Packages

As one of the most popular package manager on OS X, Homebrew is indeed a very nice tool to manage packages. However, when you want to back up your packages, or migrate the packages onto another machine, Homebrew didn’t invent such a tool. If you are moving to an OS X of the same version, you can simply copy the Homebrew directory (default is /usr/local) to your new machine. Otherwise, we need to find a way to install the exactly same packages on your new machine. In order to accomplish the goal, I wrote a small piece of bash script to generate a restore script to install the packages.

Continue reading

Use Both Homebrew and Macports on Your OS X

Homebrew and Macports are two excellent package managers on OS X. At most of the time, Homebrew is fair enough: it has a large package collections. But sometimes, there are just some packages not available in Homebrew while they are in Macports. Although having both of them installed is not recommended, I still want to give it a try.

The basic rules here are using Homebrew packages as much as possible. When one package is not available in Homebrew, install it from Macports (you will soon see why). We will wrap the executables installed by Macports with suitable Environmental Variables.

Please note that this post only presents a workaround. It is not garanteed to work, and it is possible that it does not work under some certain circumstances.

Continue reading

Install and Use GNU Command Line Tools on macOS/OS X

If you are moving onto macOS/OS X from GNU/Linux, you would probably find out that the command line tools shipped with OS X are not as powerful and easy to use as the tools in Linux. The reason is that macOS/Mac OS X uses the BSD version command line tools, which are different from the Linux version, while they are both compliant with POSIX standards. But we can easily install the GNU command line tools by using Homebrew in Mac OS X and set them as default.

Note: you need to notice that you may have some compatibility issues with shell scripts written specifically for OS X after you have replaced your OS X commands with the GNU version. Although the very vast majority of shell scripts have no problem, you just need to be aware that when there comes a problem, this may be the spot to check on.

Continue reading