50

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.

1
  • 1
    The (current-buffer) gets evaluated when your .emacs runs. You want to use (shell-command "date" t) to have it insert in the buffer which is current when you invoke the command, or encapsulate it in a defun, so that it gets evaluated when you execute the function. Commented Sep 6, 2012 at 19:18

2 Answers 2

110

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, though some situations are best served by deleting the newline at the end (e.g. in ERC, rcirc, etc.) in which case go to the end of the line/output (C-e) and then to delete the newline hidden char: C-d (this addition nicked from Mickery Petersen's Mastering Emacs site).
19

A friend of mine at work helped me.

(defun put-the-date ()
  (interactive)
  (insert (shell-command-to-string "date")))

(global-set-key
 (kbd "C-c C-d")
 'put-the-date
 )

5 Comments

There is no need to call the shell for something that can be obtained within emacs: (insert (format-time-string "%Y-%m-%d %H:%M:%S"))
Thank you, but I was just using it as an example of a shell script that outputs some kind of string... Could have been "echo hi" or "ls" really...
(global-set-key (kbd "C-c C-d") (lambda () (interactive) (insert (shell-command-to-string "date")))) uses an anonymous function and is more succint.
for the evil-mode people out there: (define-key evil-normal-state-map (kbd "SPC f d") (lambda () (interactive) (insert (shell-command-to-string "date +%F_%T"))))
@sjas I actually prefer having a separate command and keybinding despite the small extra length. 1) This is the pattern the built-in functions and most packages use. 2) Browsing the keymap (C-h m or similar) will show a useful overview, rather than an anonymous function. 3) You can find the new command when browsing available functions, e.g. when using tab completion under M-x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.