Emacs: Interactive Abbrev

By Xah Lee. Date: . Last updated: .

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

[see Emacs: Abbrev Mode]

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

emacs shell commands abbrev
xah-interactive-abbrev

Here's the code:

(defcustom xah-interactive-abbrev-alist nil "A alist for interactive abbreves. Key and value are strings. Key is for abbrev. Value is the text to be inserted." :group 'xah)

(setq xah-interactive-abbrev-alist
      '(
        ("ssh" . "ssh -l you example.org ")
        ("img1" . "convert -quality 85% ")
        ("imgScale" . "convert -scale 50% -quality 85% ")
        ("img256" . "convert +dither -colors 256 ")
        ("imgBatch" . "find . -name \"*png\" | xargs -l -i basename \"{}\" \".png\" | xargs -l -i  convert -quality 85% \"{}.png\" \"{}.jpg\"")
        ("img-bmp2png" . "find . -name \"*bmp\" | xargs -l -i basename \"{}\" \".bmp\" | xargs -l -i  convert \"{}.bmp\" \"{}.png\"")
        ("gif to webm" . "avconv -f gif -i cat.gif cat.webm")
        ("gif to mp4" . "avconv -f gif -i cat.gif cat.mp4")

        ("grep" . "grep -r -F \"xxx\" --include='*html' ~/web")
        ("firefox" . "setsid firefox &")

        ("delete empty file" . "find . -type f -empty")
        ("chmod file" . "find . -type f -exec chmod 644 {} ';'")
        ("delete emacs backup~" . "find ~/web/ -name \"*~\" -delete")
        ("find empty dir" . "find . -depth -empty -type d")
        ("delete empty dir" . "find . -depth -empty -type d -delete")

        ("empty trash" . "rm -r /home/xah/.local/share/Trash")

        ("chmod2" . "find . -type d -exec chmod 755 {} ';'")
        ("lynx" . "lynx -dump -assume_local_charset=utf-8 -display_charset=utf-8 -width=100")

        ("clojure" . "java -cp ~/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main")
        ))
(defun xah-interactive-abbrev ()
  "Prompt to insert string from a alist ‘xah-interactive-abbrev-alist’

URL ‘http://ergoemacs.org/emacs/emacs_interactive_abbrev.html’
version 2017-08-13"
  (interactive)
  (let (($x
         (ido-completing-read
          "abbrevs:"
          (mapcar (lambda (x) (car x)) xah-interactive-abbrev-alist)
          "PREDICATE" "REQUIRE-MATCH")))
    (insert (cdr (assoc $x xah-interactive-abbrev-alist)))))

You must give a easy key for this command, such as F9.

(global-set-key (kbd "<f9>") 'xah-interactive-abbrev)

[see Emacs: How to Define Keys]

Also, modify the xah-interactive-abbrev-alist for your own abbrevs.

Shell Topic

  1. Emacs: Run Shell in Emacs
  2. Why Run Shell in Emacs?
  3. Emacs: Difference between shell, term, eshell
  4. Emacs: M-x eshell
  5. Emacs: View Linux man Page
  6. Emacs: Interactive Abbrev
  7. Emacs: File Manager, dired
  8. Emacs: Batch Rename Files
  9. Linux: Basic Shell Commands
Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.