Emacs: Interactive Abbrev 🚀

By Xah Lee. Date: . Last updated: .

If you have a hundred Abbrevs , one problem is that you forgot what's the abbrev.

Solution is to have a prompt listing all abbreves. A Interactive abbrev.

emacs interactive abbrev 2022-06-02

Here's the code:

(defvar xah-interactive-abbrev-hashtable (make-hash-table :test 'equal)
  "A hashtable for `xah-interactive-abbrev'.
Key is abbrev string.
Value is the text to be inserted.
Version: 2023-08-01")

(puthash "optimize png" "oxipng -o 4 --strip safe *png" xah-interactive-abbrev-hashtable)
(puthash "delete metadata" "exiftool -all= -overwrite_original *png *jpg" xah-interactive-abbrev-hashtable)
(puthash "gif to mp4" "ffmpeg -f gif -i x.gif x.mp4" xah-interactive-abbrev-hashtable)
(puthash "gif to webm" "ffmpeg -f gif -i x.gif x.webm" xah-interactive-abbrev-hashtable)
(puthash "gitdiff" "git --no-pager diff --color --no-index f1 f2" xah-interactive-abbrev-hashtable)

(puthash "git archive" "git archive -o ../xxgit.zip HEAD" xah-interactive-abbrev-hashtable)

(puthash "image 256" "magick convert +dither -colors 256 " xah-interactive-abbrev-hashtable)
(puthash "image convert" "magick convert -quality 85% " xah-interactive-abbrev-hashtable)
(puthash "image scale" "magick convert -scale 50% -quality 85% " xah-interactive-abbrev-hashtable)
(puthash "lynx" "lynx -dump -assume_local_charset=utf-8 -display_charset=utf-8 -width=76 xxxxxxurl > xxfileName.txt" xah-interactive-abbrev-hashtable)
(puthash "mov to mp4" "ffmpeg -f mov -i x.mov x.mp4" xah-interactive-abbrev-hashtable)

(puthash "unix chmod file" "find . -type f -exec chmod 644 {} ';'" xah-interactive-abbrev-hashtable)
(puthash "unix chmod2" "find . -type d -exec chmod 755 {} ';'" xah-interactive-abbrev-hashtable)
(puthash "unix delete emacs backup~" "find . -name \"*~\" -delete" xah-interactive-abbrev-hashtable)
(puthash "unix delete empty dir" "find . -depth -empty -type d -delete" xah-interactive-abbrev-hashtable)
(puthash "unix delete empty file" "find . -type f -empty" xah-interactive-abbrev-hashtable)
(puthash "unix delete mac DS_Store" "find . -name \".DS_Store\" -delete;" xah-interactive-abbrev-hashtable)
(puthash "unix delete mac __MACOSX" "find . -depth -name \"__MACOSX\" -type d -exec rm -rf {} ';'" xah-interactive-abbrev-hashtable)
(puthash "unix grep" "grep -r -F \"hhhh\" --include='*html' ~/web" xah-interactive-abbrev-hashtable)
(puthash "unix image -bmp2png" "find . -name \"*bmp\" | xargs -l -i basename \"{}\" \".bmp\" | xargs -l -i magick convert \"{}.bmp\" \"{}.png\"" xah-interactive-abbrev-hashtable)
(puthash "unix image Batch" "find . -name \"*png\" | xargs -l -i basename \"{}\" \".png\" | xargs -l -i magick convert -quality 85% \"{}.png\" \"{}.jpg\"" xah-interactive-abbrev-hashtable)
(puthash "unix list empty dir" "find . -depth -empty -type d" xah-interactive-abbrev-hashtable)

(puthash "ytd" "youtube-dl -f 'bestvideo,bestaudio' -o 'f%(format_id)s.%(ext)s' url" xah-interactive-abbrev-hashtable)

(puthash "pwsh subdir size" "dir -Directory | % { Write-Host $_.name \" \" -NoNewLine; \"{0:N0}\" -f ((dir $_ -File -Recurse | measure -Property Length -sum).sum / 1mb); }" xah-interactive-abbrev-hashtable)
(puthash "pwsh diff" "diff (cat f1) (cat f2) " xah-interactive-abbrev-hashtable)
(puthash "pwsh emacs backup~" "dir -Recurse -File -Filter *~ | % {$_.fullname}" xah-interactive-abbrev-hashtable)
(puthash "pwsh .DS_Store" "dir -Recurse -File -Filter .DS_Store | % {$_.fullname}" xah-interactive-abbrev-hashtable)
(puthash "pwsh #emacs_auto_save#" "dir -Recurse -File -Filter '#*#'" xah-interactive-abbrev-hashtable)
(puthash "pwsh htaccess" "dir -Recurse -File -Filter .htaccess" xah-interactive-abbrev-hashtable)
(puthash "pwsh dir" "dir -Recurse -File -Filter xx* | % {$_.fullname}" xah-interactive-abbrev-hashtable)
(defun xah-interactive-abbrev ()
  "Prompt to insert string from `xah-interactive-abbrev-hashtable'.

URL `http://xahlee.info/emacs/emacs/emacs_interactive_abbrev.html'
Created: 2017-08-13
Version: 2022-04-16"
  (require 'subr-x)
  (let ((xinput
          (hash-table-keys xah-interactive-abbrev-hashtable) nil t)))
    (insert (gethash xinput xah-interactive-abbrev-hashtable))))

You should have a easy key for this command, such as F9. [see Emacs Keys: Define Key]

also, you should use fido-vertical-mode or similar for vertical list.

Emacs, using abbrev mode