Author Archives: Hong

About Hong

The maintainer of topbug.net.

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 February 5, 2020

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

How to Achieve Different Open Source Levels on Your Laptop/Desktop

Open-source software, sometimes referred as free software, is a kind of software that permits the users to run, copy, distribute, study, change and improve it. Examples include Mozilla Firefox, Android, etc. For whatever reasons, many people care about whether the software they installed is open-source, while others don't. Here I'll show you how to achieve different open source levels, from 0% open source to 100% open source.

Continue reading

Optimization for Mobile: A Pair Programming Story at FullStackLA

Last updated on October 1, 2016

Recently, I had the opportunity to pair with two fantastic developers: Ben @benjaminma and Ian @IanDCarroll at FullStackLA. This was also my first time at FullStackLA and it was great experience.

Ian had been working on a static website which lists and displays all the local meetups in Los Angeles. The challenge was simple: improve the website to look better on a mobile phone.

Continue reading

dired-quick-sort: Sort Dired Buffers Quickly in Emacs

Last updated on December 10, 2016

While Dired is great for browsing the file system, it is often annoying that the buffer is not sorted in the way we want to see. While Dired provides the flexible customizing variable dired-listing-switches, it is still not convenient to switch between different sorting criteria quickly. For this reason, I created the Emacs extension dired-quick-sort to make sorting Dired an easy story.

Project Homepage

Screenshot

3 Common Misconceptions about Data Backup

Last updated on November 23, 2018

Backup is undoubtedly important for most people with digital data. However, individuals often risk data loss by using dangerous strategies. Here, I will show a few false (and dangerous) notions that people often hold for their data backup.

“External Hard Drives are Enough for Backup”

While you may be well aware of the possibility of external hard drive breaking down and make backups on multiple external hard drives, but it is certainly not enough. Usually, when you make backup on external hard drives or optical discs, you typically store them in your home or in the office. But some extreme situations can happen: fire, theft, earthquake, or some magnets that break all the drives. It is usually considered much safer to make remote backup along with these external drives. To make remote backup, you can either use a simple method such as uploading files to cloud storage websites like Dropbox or OneDrive, or use a more sophisticated approach to make frequent backup easier: use incremental backup software such as Areca Backup or Back In Time along with a backup virtual private server (VPS) such as Backupsy and Time4VPS.

“File Synchronization Software is a Reliable Backup Tool”

Many people heavily rely on file synchronization software such as Dropbox, Google Drive, Bit Torrent Sync, Syncthing, etc. to back up. I would say they aren't really reliable: the data can be lost forever in many circumstances. For example, a software bug, or a corruption of your local disk, may well affect the remote copy that you may even not know for a long time. While you can still use these software as convenient “backup” tools, my recommendation is to make sure you make snapshots: you can always go back if you found a certain file is corrupted. In order for it to be easier to make snapshots, you may wanna try some incremental backup software such as Areca Backup on Windows, Time Machine on Mac OS X or Back In Time on GNU/Linux.

“No Need to Back up Things on the Internet”

Anything on the Internet may disappear someday—even this blog post you are reading. People remove articles, videos and other contents on the Internet all the time for various reasons: commercial purpose (e.g., free contents become paid), copyright infringement, or the content owner simply goes bankrupted. If you really don't want to lose access to some Internet contents, you probably want to make a local copy and make proper backup copies. For example, for YouTube videos, you can download them via sites like savefrom.net or a software like youtube-dl; for an article, you can save the web page as a PDF file; for an image, you can save them to your local disk by right clicking them in your browser.

Security Checks to Do Before Installing an Android App

Last updated on August 21, 2016

Android is one of the most popular mobile operating systems. However, some Android apps can cause security issues if not carefully handled. They can be badly written which leaves a lot of security holes for intruders, or itself acts like a malware/virus. Therefore, before installing an Android app, one need to do some checks to make sure it won't cause security issues.

Continue reading