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
.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With