{´◕ ◡ ◕｀} This requires both expect and tmux to be installed. They are only used in the background. You can use this function with GUI emacs fine. No other dependencies are required for this elisp function to work. The bash/tcl/expect script is embedded in elisp. Update The builtin edmacro-format-keys gives me the functionality I want without the rigmarole.

make-kbd-from-string is a function that takes a string literal and gives you a keyboard macro.

I don’t know if it’s possible to do this in emacs lisp. What I ended up doing was making an expect script that spawns a vanilla emacs and runs kmacro-start-macro .

tcl/expect then enters the literal string in and emacs saves the recorded macro to a temporary file which emacs retrieves.

All of this happens inside a temporary tmux window in the background. That was needed because the shell-command function would usually hang if asked to run a program that requires a tty .

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 (defun e/chomp (str) "Chomp leading and tailing whitespace from STR." (while ( string-match "\\ `

+\\|^\\s-+\\|\\s-+$\\|

+\\' " str) (setq str ( replace-match "" t t str))) str) (defun make-kbd-from-string (s) (let ((quoted-string (let ((print-escape-newlines t )) ( prin1-to-string s))) (tf (make-temp-file "emacskbm" nil ".exp" ))) (ignore-errors (with-temp-buffer ( insert ( concat "outfile=/tmp/emacskbm.txt

" "rm -f \"$outfile\"

" "

" "cat > /tmp/emacskbm.exp <<HEREDOC

" "if { \\$argc >= 1 } {

" " set literal [lindex \\$argv 0]

" "}

" "

" "spawn sh

" "send -- \"emacs -Q -nw\"

" "send -- \\015

" "expect -exact \"scratch\"

" "send -- \\030

" "send -- \"(\"

" "send -- \"\\$literal\"

" "send -- \\030

" "send -- \")\"

" "send -- \\033:

" "send -- \"(with-temp-buffer (insert (replace-regexp-in-string \\\"^Last macro: \\\" \\\"\\\" (kmacro-view-macro))) (write-file \\\"$outfile\\\"))\"

" "send -- \\015

" "send -- \\033:

" "send -- \"(kill-emacs)\"

" "send -- \\015

" "send -- \\004

" "interact

" "HEREDOC

" "

" "{

" "expect -f /tmp/emacskbm.exp \"$@\"

" "} &>/dev/null

" "tmux wait-for -S emacskbm

" )) (write-file tf))) (shell-command ( concat "tmux neww -d bash " tf " " quoted-string "; tmux wait-for emacskbm" )) (e/chomp (with-temp-buffer ( insert-file-contents "/tmp/emacskbm.txt" ) ( buffer-string )))))

How to use this function:

1 (make-kbd-from-string "/msg yo hi" )

An example of output

1 /msg SPC yo SPC hi

On second thoughts, I suppose this could’ve all been done through term-mode if you had a term-mode inside of a term-mode in which case it could all be done in emacs-lisp. Do I want to find out? I’m not sure.

What is the whole point of this function you may ask? The issue is you can’t (insert "some text") inside of term . You must use a keyboard macro.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (defun irssi-search-channels (pattern) (interactive ( list ( read-string "pattern:" ))) ( execute-kbd-macro (kbd "M-7" )) ( execute-kbd-macro (kbd "C-a C-k" )) ( execute-kbd-macro (kbd (make-kbd-from-string ( concat "/msg alis LIST * -topic " pattern)))) ( execute-kbd-macro (kbd "C-m" )) ( execute-kbd-macro (kbd (make-kbd-from-string "/query alis" ))) ( execute-kbd-macro (kbd "C-m" ))) ( define-key irssi-term-mode-map (kbd "M-/" ) #' irssi-search-channels)

Demonstration

Extra stuff

expect code generation script

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/bin/bash export TTY ( hs " $( basename " $0 " ) " " $@ " "#" "<==" " $( ps -o comm = $PPID ) " 0</dev/null ) &>/dev/null s = " $1 " fp = /tmp/emacskbm.txt { unbuffer x \ -sh "emacs -Q -nw" \ -e scratch \ -c x \ -s "(" \ -s " $1 " \ -c x \ -s ")" \ -m : -s "(with-temp-buffer (insert (replace-regexp-in-string \"^Last macro: \" \"\" (kmacro-view-macro))) (write-file \" $fp \"))" -c m \ -m : -s "(kill-emacs)" -c m \ -i } &>/dev/null cat " $fp "

The more terse version

It’s operational slowness is made up for by its caching.