Emacs: Interactive Abbrev

By Xah Lee. Date: .

Using Abbrev for Shell Commands

You can define abbrev for frequently used shell commands. For example, i type “3rs ” and it expands to

rsync -z -r -v -t --chmod=Dugo+x --chmod=ugo+r --delete --exclude='*~' --exclude='.bash_history' --exclude='logs/'  --rsh='ssh -l u89150' ~/web/ u89150@s72750.example.com:~/

〔►see Linux: Sync Files Across Machines: rsync Tutorial

Is emacs abbrev better than bash alias?

Emacs abbrev is better, because it expands to the full command. Full commands lets you see what you are actually doing, instead of short abbrev. You can also edit it if you want.

The shell alias execute directly without expansion.

〔►see Emacs Abbrev Tutorial

What about using shell 【Ctrl+r】 backward search feature?

That's great, but for frequently used commands, alias or abbrev is better, because you get EXACTLY the command you want, less keystrokes, and no eye-balling, just muscle memory in typing keys.

With back search, you might have variations of long commands, and you have to eye-ball to be sure it's the command you want. Bash alias or emacs abbrev are muscle memory.

What's the advantage of using shell inside emacs?

Easier copy/paste, full editing power of emacs, no need to switch to terminal, automatic session log. 〔►see Emacs Inferior Shell vs Terminal: What's the advantage of running shell inside emacs?

Interactive Abbrev in Shell

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

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

emacs shell commands abbrev
Shell commands abbrev with ido selection interface. xah-shell-commands.

So, here's a command for interactive abbrev:

(defcustom xah-shell-abbrev-alist nil "alist of xah's shell abbrevs")

(setq xah-shell-abbrev-alist
      '(
        ("rsync1" . "rsync -z -r -v -t --chmod=Dugo+x --chmod=ugo+r --delete --exclude='*~' --exclude='.bash_history' --exclude='logs/'  --rsh='ssh -l u80781' ~/web/ u80781@s30097.example.com:~/")

        ("ssh" . "ssh -l u80781 xahlee.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\"")

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

        ("rm empty" . "find . -type f -empty")
        ("chmod file" . "find . -type f -exec chmod 644 {} ';'")
        ("rm~" . "find . -name \"*~\" -exec rm {} ';'")
        ("findEmptyDir" . "find . -depth -empty -type d")
        ("rmEmptyDir" . "find . -depth -empty -type d -exec rmdir {} ';'")
        ("chmod2" . "find . -type d -exec chmod 755 {} ';'")
        ("lynx" . "lynx -dump -assume_local_charset=utf-8 -display_charset=utf-8 -width=100")
        ("vp" . "feh --randomize --recursive --auto-zoom --action \"gvfs-trash '%f'\" --geometry 1600x1000 ~/Pictures/ &")))

(defun xah-shell-commands (*cmd-abbrev)
  "insert shell command from a list of abbrevs.

URL `http://ergoemacs.org/misc/emacs_abbrev_shell_elisp.html'
version 2015-02-05"
  (interactive
   (list
    (ido-completing-read "shell abbrevs:" (mapcar (lambda (x) (car x)) xah-shell-abbrev-alist) "PREDICATE" "REQUIRE-MATCH")))
  (progn
    (insert (cdr (assoc *cmd-abbrev xah-shell-abbrev-alist)))))

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

(global-set-key (kbd "<f9>") 'xah-shell-commands)

〔►see Emacs: How to Define Keys

xah-shell-commands has these advantages:

Like it? Buy Xah Emacs Tutorial. Thanks.