HOME
Updated: 2023-01-20

Useful Tips and Tricks

This page is a collection of random information that I have found useful, perhaps you may find something useful too.

Unix / Linux

Ascii table

Need to lookup an ASCII character number run man ascii and most Unix-like systems will have a full ASCII table.

Emacs

Get the directory location of an elisp script

[2023-01-20 Fri]

Sometimes when writing an Emacs Lisp script it is useful to use have the script's directory location. Unfortunately there is not one foolproof solution for this.

default-directory is a variable defined in ‘src/buffer.c’.

Name of default directory of current buffer. It should be an absolute directory name; on GNU and Unix systems, these names start with ‘/’ or ‘~’ and end with ‘/’. To interactively change the default directory, use command ‘cd’.

At first I used the default-directory variable. However, this only works if you are opening the file and evaluating the buffer manually. That is because it is the current working directory of Emacs.

For example, if we have a file /tmp/test.el containing:

(message default-directory)

Running the command emacs --script /tmp/test.el from the home directory will output ~/.

So we need another solution for scripts since they could be run from anywhere. Trey Jackson's Stack Overflow answer provides a good alternative load-file-name.

load-file-name is a variable defined in ‘src/lread.c’.

Full name of file being loaded by ‘load’.

With our last example, changing the file contents to (message load-file-name) and running emacs --script /tmp/test2.el will output:

/tmp/test2.el

Now this will only work when evaluating a file as a --script or by using load from elisp. Using eval-buffer it will be nil. So we can combine the two solutions:

(defun my/get-script-directory ()
  (if (null load-file-name)
      (expand-file-name default-directory)
    (file-name-directory load-file-name)))

I've opened a post on reddit to see if anyone has a better solution.

Concatenate directory names in elisp

[2022-04-27 Wed]

Use file-name-concat or file-name-as-directory in conjunction with usual string concatenation functions. These function are portable across operating systems.

(file-name-as-directory "~/Downloads")
;; => "~Downloads/"

(concat (file-name-as-directory "Downloads") "debian.iso")
;; => "Downloads/debian.iso"

;; New in Emacs 28.1
(file-name-concat "/home/bob" "Documents" "file.org")
;; => "/home/bob/Documents/file.org"

For more info see the Directory Names section in the Elisp Manual.

Good Font for Emacs on MS Windows

I use Consolas which looks pretty sharp, better than Lucida Console which is what I used before.

Lookup C Documentation

Recently discovered there is a way to jump to the libc manual (if installed) for a given function. Could potentially work with other languages with info reference manuals, if there are any.

  • C-h S info-lookup-symbol

Display the definition of SYMBOL, as found in the relevant manual. When this command is called interactively, it reads SYMBOL from the minibuffer. In the minibuffer, use M-n to yank the default argument value into the minibuffer so you can edit it. The default symbol is the one found at point.

Nice in conjunction with M-x man which I have bound to 'C-x m'.

Convert between binary and decimal in calc

Source: Xah Lee's page on converting decimal to hexadecimal numbers

  • type 'd2' to binary
  • type 'd6' to hexadecimal
  • type 'd0' to decimal

These only change how numbers are displayed, input numbers are still read in decimal.

To input numbers in the current base type '#'.

Remove documentation links from gopls in eglot

Very important to use :json-false to avoid parsing errors.

(setq-default eglot-workspace-configuration
            '((:gopls . ((linksInHover . :json-false)))))

Internet

Recursively sync directory with FTP

Source: Server Fault: How to upload a directory recursively to an FTP server by just using ftp or lftp?

lftp -e "mirror -R [source [target]]" -u [username],[password] [host]

The -R means reverse, because the mirror command normally works from server to local.

lftp's mirror command has a wide breadth of options that can be used to fit your needs. In my case I was trying to replicate rsync style syncing for a system that I only had FTP access to.

Comments

Email comments and corrections to comment@taingram.org.

Submissions may be posted publicly unless requested otherwise in your email.