4

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

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
traday
  • 1,151
  • 11
  • 21

2 Answers2

16

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.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • LOL, since this is my first question, I don't have enough rep points to vote your answer up. – traday Nov 07 '10 at 02:58
3

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?

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163