Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending commands to shell with emacs lisp

568 views
Skip to first unread message

Corey Foote

unread,
Sep 13, 2009, 1:15:38 AM9/13/09
to help-gn...@gnu.org
I'm trying to send commands to a shell buffer. I'm able to send the command "hello world" by calling this:

(defun exec-command ()
  (interactive)
  (set-buffer (get-buffer "*shell*"))
  (insert "hello world"))

But I can't figure out how to send over the "enter key" to actually make it execute the shell command. I'm an emacs lisp newbie!

Thanks everybody!

- Corey


Hotmail: Free, trusted and rich email service. Get it now.

to...@tuxteam.de

unread,
Sep 13, 2009, 1:45:08 AM9/13/09
to Corey Foote, help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, Sep 13, 2009 at 01:15:38AM -0400, Corey Foote wrote:
>
> I'm trying to send commands to a shell buffer. I'm able to send the command "hello world" by calling this:
>
> (defun exec-command ()
> (interactive)
> (set-buffer (get-buffer "*shell*"))
> (insert "hello world"))
>
> But I can't figure out how to send over the "enter key" to actually make it execute the shell command. I'm an emacs lisp newbie!

I think what you are looking for is "call-process" and "start-process".

Evaluating

(call-process "ls" nil t t "-l")

for example, you get the current directory listing in long form inserted
at point into the current buffer.

(call-process waits for the process to terminate, while start-process
just lets the process run while Emacs goes on with its other tasks).

There are so many options and variants that I'll stop here and refer you
to the documentation -- but do ask here if you get stuck.

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFKrIbkBcgs9XrR2kYRAvHvAJ4iqHDOcRWoJz+861efBHlUMn9qBACfRUgI
pjtfndJKqUQ6W7HTCkmtGgg=
=V2HY
-----END PGP SIGNATURE-----


Corey Foote

unread,
Sep 13, 2009, 2:43:29 AM9/13/09
to to...@tuxteam.de, help-gn...@gnu.org
Thanks, but I really am just looking for a way to send an "enter" keystroke to my *shell* buffer after I've inserted a command. I'm just trying to save myself a few keystrokes in my edit, compile, execute cycle.

> Date: Sun, 13 Sep 2009 07:45:08 +0200
> From: to...@tuxteam.de
> To: corey...@hotmail.com
> CC: help-gn...@gnu.org
> Subject: Re: sending commands to shell with emacs lisp

Ready for Fall shows? Use Bing to find helpful ratings and reviews on digital tv's. Click here.

Thierry Volpiatto

unread,
Sep 13, 2009, 2:55:50 AM9/13/09
to help-gn...@gnu.org
Hi,
Corey Foote <corey...@hotmail.com> writes:

> I'm trying to send commands to a shell buffer. I'm able to send the command "hello world" by calling this:
>
> (defun exec-command ()
> (interactive)
> (set-buffer (get-buffer "*shell*"))
> (insert "hello world"))

You probably want:

,----
| (with-current-buffer "*shell*"
| (goto-char (point-max))
| (insert "echo hello world"))
`----

Or

,----
| (switch-to-buffer "*shell*"
| (goto-char (point-max))
| (insert "echo hello world"))
`----

All that assume shell is started.
If not just run the shell you want from your function before inserting.


If you want a complete mode to send commands to a shell (even an xterm)
from emacs, have a look at eev-mode.
http://www.emacswiki.org/cgi-bin/wiki/EevMode

> But I can't figure out how to send over the "enter key" to actually make it execute the shell command. I'm an emacs lisp newbie!

If you use eshell as shell, the command is `eshell-send-input', but i
think it's better to use enter to be sure you really want to send the
command inserted.

But maybe what you only need is instead of all that is:

(shell-command "echo hello world")

Put point at end of this expression and hit C-x C-e.


> Thanks everybody!
>
> - Corey
>
> _________________________________________________________________


> Hotmail: Free, trusted and rich email service.

> http://clk.atdmt.com/GBL/go/171222984/direct/01/

--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France

Bruno Barbier

unread,
Sep 13, 2009, 3:45:36 AM9/13/09
to
On 2009-09-13, to...@tuxteam.de <to...@tuxteam.de> wrote:
>
> On Sun, Sep 13, 2009 at 01:15:38AM -0400, Corey Foote wrote:
>>
>> I'm trying to send commands to a shell buffer. I'm able to send the
>> command "hello world" by calling this:
>>
>> (defun exec-command ()
>> (interactive)
>> (set-buffer (get-buffer "*shell*"))
>> (insert "hello world"))
>>
>> But I can't figure out how to send over the "enter key" to actually
>> make it execute the shell command. I'm an emacs lisp newbie!

I'm using the following code to send a command to an existing shell:

(with-current-buffer buffer
(let ((process (get-buffer-process (current-buffer)))
)
(unless process
(error "No process in %s" buffer-or-name))
(goto-char (process-mark process))
(insert command-string)
(comint-send-input nil
t ;; artificial
)
))

so, the magic is:
(comint-send-input nil t)

When I'm not using a shell and I just want the shell command result,
I'm doing:

(with-output-to-temp-buffer "*shell result*"
(shell-command "echo hello world" "*shell result*")
)

Helmut Eller

unread,
Sep 13, 2009, 3:14:42 AM9/13/09
to
* Corey Foote [2009-09-13 07:15+0200] writes:

> I'm trying to send commands to a shell buffer. I'm able to send the command
> "hello world" by calling this:
>
> (defun exec-command ()
> (interactive)
> (set-buffer (get-buffer "*shell*"))
> (insert "hello world"))
>
> But I can't figure out how to send over the "enter key" to actually make it
> execute the shell command. I'm an emacs lisp newbie!

comint-send-input sends the region (and a newline) to the subprocess.
So:

(defun exec-command ()
(interactive)
(set-buffer (get-buffer "*shell*"))
(insert "hello world")

(comint-send-input))

should work.

Helmut

to...@tuxteam.de

unread,
Sep 13, 2009, 6:21:42 AM9/13/09
to Corey Foote, help-gn...@gnu.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sun, Sep 13, 2009 at 02:43:29AM -0400, Corey Foote wrote:
>
> Thanks, but I really am just looking for a way to send an "enter" keystroke to my *shell* buffer [...]

I see. There are other useful suggestions as to what to do in that case.
My approach would have been to look at what a shell buffer does when
ENTER is hit. You can peek at that by hitting "C-h k" (aka "describe
key") in a shell buffer and then hitting ENTER. You get:

RET (translated from <return>) runs the command comint-send-input,
which is an interactive compiled Lisp function in `comint.el'.

(which is what Helmut proposes in this thread).

Regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFKrMe2Bcgs9XrR2kYRAgfkAJ0ZGUAVGzcSli0qeiFtZx+6vrxMLQCdF40S
cq0Gum4wk+CPeN5C5rRSJ+U=
=nmDp
-----END PGP SIGNATURE-----


des...@verizon.net

unread,
Sep 13, 2009, 10:39:27 AM9/13/09
to
Corey Foote <corey...@hotmail.com> writes:

> Thanks, but I really am just looking for a way to send an "enter"
> keystroke to my *shell* buffer after I've inserted a command. I'm just
> trying to save myself a few keystrokes in my edit, compile, execute cycle.

For that, you want to use a Makefile and M-x compile.

For example, in the Makefile:

all: foo.test

foo: foo.c
cc foo.c -o foo

foo.test: foo
foo args


With that Makefile, just do M-x compile and your module
is compiled if need be, then the test is run.

You might use diff in the .test rule to capture the test output
and verify the results.

I've got M-x compile bound to F1 and M-x next-error bound to
F2.

So typically, for do the edit/compile/execute cycle, I finish
my edits, Hit F1, return, and I'm done.

An important advantage with M-x compile is that Emacs will
ask you if you want to save the file before the compile starts.
It's not necessary to remember to save the file.


0 new messages