Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert the output of shell command into emacs buffer

Tags:

emacs

I want to set a key binding to insert the date into the buffer. I have written the following lisp in my .emacs file. Using date as an example:

;;put the date                                                                   (global-set-key  (kbd "C-c C-d")  (shell-command "date" (current-buffer)) ) 

The key binding works okay when I use other commands like 'next-line, but shell-command will put it into the *scratch* buffer when the .emacs is read and leaves it at that.

Maybe I need to use shell-command-on-region.

like image 498
huwr Avatar asked Sep 06 '12 02:09

huwr


People also ask

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

How do I get shells in Emacs?

You can start an interactive shell in Emacs by typing M-x shell . By default, this will start the standard Windows shell cmd.exe . Emacs uses the SHELL environment variable to determine which program to use as the shell.

How do you type commands in Emacs?

The Control key is usually labeled 'Control' or 'CTRL' and is held down while typing the other keys of the command. To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt.

What is shell Emacs?

The terminal emulator reads control codes and updates a grid of characters split into columns and rows. That, in a nutshell, is all it does. A shell is something like bash , or cmd.exe , or python . Emacs can do both. But first, it's worth talking about how Emacs interacts with external programs.


1 Answers

For the general case of inserting any output of a shell command to the current buffer, you can use the in-built keyboard chords:

C-u M-! <shell-command> 

which runs the same shell-command function, and also inserts the output back at the point in the current buffer.

The entire key-stroke itself can be saved as a macro (and perhaps assigned to a shortcut) for easier invocation of common shell commands.

like image 142
Anupam Avatar answered Sep 21 '22 10:09

Anupam