Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define an Emacs Lisp function to spawn a shell buffer with a particular command executed in the shell?

Tags:

emacs

I am doing Rails development and find that I need to spawn a shell, rename the buffer (e.g. webrick), then kick off the command (rails s) and then do the whole thing over again if I want a rails console or rails dbconsole, rspec, spork, etc. every time I start up emacs.

I am hoping 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 go away when it exits because the output in the shell buffer is important and some times I need to kill it and restart it but I don't want to lose that history.
Essentially, I want to take the manual process and make it invokable.

Any help is much appreciated

Tom

like image 200
traday Avatar asked Dec 01 '22 04:12

traday


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 prompts for a name to use when you run it interactively (M-x spawn-shell). It creates a new buffer based on the input name using generate-new-buffer-name, and you were missing the newlines on the end of the strings you were sending to the process.

like image 130
Trey Jackson Avatar answered Apr 26 '23 03:04

Trey Jackson


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

like image 30
Gareth Rees Avatar answered Apr 26 '23 03:04

Gareth Rees