Category Archives: UNIX variants

UNIX variants (or formally POSIX-compliant systems), a family of operating systems.

Set up Synapse (Matrix Homeserver) on Ubuntu 20.04/16.04

Last updated on May 5, 2021

Matrix is an open standard for decentralized persistent communication, shares somewhat similar goals to Jabber/XMPP. It attracts people from using centralized communicating software such as Facebook Messenger, WhatsApp, etc. In the Matrix protocol, a piece of software called "homeserver" plays a key role to connect users. To use Matrix, one such server must be set up. In this post, we will set up Synapse, an implementation of the Matrix homeserver maintained by the Matrix team, using a minimal configuration on Ubuntu 16.04/20.04.

Continue reading

A Better ls Command

Last updated on April 14, 2024

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

Screenshots:

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

Prevent Browser Processes Started by Emacs from Being Killed When Emacs Exits on GNU/Linux

Last updated on November 16, 2016

The function browse-url in Emacs can be used to start a browser process to visit a given URL. It is used by many packages such as org-mode, mu4e, etc. However, on GNU/Linux, by default a new browser process started by browse-url will be killed if the Emacs process exits. To prevent the browser process from being killed, add the following code to your Emacs init file:

(when (and (executable-find "setsid") (executable-find "gnome-open"))
  (setq browse-url-browser-function
        (lambda (url &optional ignored)
          (start-process "" nil "setsid" "gnome-open" url))))

You will need both the setsid and gnome-open commands available. Note that replacing gnome-open with xdg-open is not guaranteed to work.

Installing Emacs from Source: Avoid the Conflict of ctags

Last updated on December 10, 2016

If installing Emacs from source, an executable named ctags will be installed by default. However, many systems such as GNU/Linux, FreeBSD and macOS often already have an executable named ctags installed, such as Exuberant Ctags or Universal Ctags, which then causes conflict. To avoid this conflict, we can use the switch --program-transform-name='s/^ctags$/ctags.emacs/' when running configure to rename ctags to ctags.emacs. For example:

cd /path/to/emacs-source
mkdir build && cd build
../configure --program-transform-name='s/^ctags$/ctags.emacs/'

After that, running make install will install the Emacs ctags executable as ctags.emacs.

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

Last updated on December 10, 2016

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

Last updated on December 10, 2016

In zsh, it is often annoying that we can't easily restore the command we just canceled by Ctrl-C: 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 Ctrl-C for more than once—but ZLE_LINE_ABORTED would become empty if Ctrl-C 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
    MY_LINE_ABORTED="$ZLE_LINE_ABORTED"
  fi

  # Restore aborted line on the first undo.
  if [[ -n $MY_LINE_ABORTED ]]; then
    local savebuf="$BUFFER" savecur="$CURSOR"
    BUFFER="$MY_LINE_ABORTED"
    CURSOR="$#BUFFER"
    zle split-undo
    BUFFER="$savebuf" CURSOR="$savecur"
  fi
}
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 Ctrl-C 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.

References

Emacs: Disable Certain Pairs for electric-pair-mode

Last updated on December 10, 2016

In GNU Emacs, electric-pair-mode is a minor mode for auto closing pairs of (curly) braces/brackets/quotes, which was first introduced in Emacs 24. However, up till now, it still has not provided an easy option to disable a certain pair—we need to make use of electric-pair-inhibit-predicate. To disable a certain pair, such as double quotes, we can add the following to ~/.emacs or ~/.emacs.d/init.el:

(setq electric-pair-inhibit-predicate
      `(lambda (c)
         (if (char-equal c ?\") t (,electric-pair-inhibit-predicate c))))

To disable a certain pair for a specific mode, for example to disable the pairing of {} in web-mode (to allow web-mode to better handle the auto pairing of the template tags {% %}), we can add the following to ~/.emacs or ~/.emacs.d/init.el:

;; disable {} auto pairing in electric-pair-mode for web-mode
(add-hook
 'web-mode-hook
 (lambda ()
   (setq-local electric-pair-inhibit-predicate
               `(lambda (c)
                  (if (char-equal c ?{) t (,electric-pair-inhibit-predicate c))))))

For more information, please move onto the documentation of the variable electric-pair-inhibit-predicate.

Make the less Command More Powerful

Last updated on December 25, 2024

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