How to define an Emacs Lisp function to create a shell buffer using a specific command executed in the shell?

I am developing Rails and find that I need to create a wrapper, rename the buffer (like webrick) and then run the command (rails s) and then do it all over again if I want a rails console or rails dbconsole, rspec, spork and etc. every time i run emacs.

I hope for something like this:

(defun spawn-shell () "Invoke shell test" (with-temp-buffer (shell (current-buffer)) (process-send-string nil "echo 'test1'") (process-send-string nil "echo 'test2'"))) 

I don’t want the shell to leave when it exits because the output in the shell buffer is important, and sometimes I need to kill and restart it, but I don’t want to lose this story. Essentially, I want to take the manual process and make it invokable.

Any help is much appreciated

Tom

+4
source share
2 answers

Perhaps this version of spawn-shell will do what you want:

 (defun spawn-shell (name) "Invoke shell test" (interactive "MName of shell buffer to create: ") (pop-to-buffer (get-buffer-create (generate-new-buffer-name name))) (shell (current-buffer)) (process-send-string nil "echo 'test1'\n") (process-send-string nil "echo 'test2'\n")) 

It asks for a name to use during interactive startup ( Mx spawn-shell ). It creates a new buffer based on the input name using generate-new-buffer-name , and you did not have enough newline lines at the end of the lines you sent to the process.

+16
source

If your only problem is that the shell buffer disappears after executing the commands, why not use get-buffer-create instead of with-temp-buffer ?

+3
source

All Articles