How to execute a command within the emacs shell which was passed as command line argument

This topic is in fact not so closely related to astronomy. However, from a technical perspective I think it still fits onto this blog. Recently I was in a situation where I wanted to start a console command within the emacs shell – but directly via the command line. The xterm shell has the -e option for this purpose. Also the bash command provides the -c option. However, in emacs I was not able to find such an option. I also didn’t want to dive too deep into the LISP stuff. If you are in a similar situation the following solution might be what you need. A very small emacs LISP script will do the job:

(toggle-truncate-lines 1)
(insert (elt argv 0))

This script first opens a new shell in emacs and then inserts the first argument passed to the script into the shell. The last line then executes the command. In the end emacs can be called the following way:

emacs --load exec_cmd.el "<my command>"

The following example calls xeyes with red eyes – this shows that passing parameters to the executable is also not a problem:

emacs --load exec_cmd.el "xeyes -center red"

This little trick helped me a lot since navigating and especially searching inside emacs is much more convenient than in xterm (at least in my eyes).

Last updated: June 16, 2022 at 13:33 pm