Insert the output of shell command into emacs buffer - 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.

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.

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
)

Related

Emacs lisp key binding in shell mode not working

I have my emacs editor setup to run a shell when I press Ctrl-z. When inside of a shell buffer, I use Ctrl-z to clear the buffer by running the erase-buffer function. The code is being evaluated (when I Ctrl-h v and describe the shell-mode-map I can see that C-z is bound to clear-shell-buffer in shell mode. When I run the clear-shell-buffer with M-x the message says:
You can run the command clear-shell-buffer with <C-z>
However, when I type Ctrl-z in the shell it does not run the function or give any messages at all. Any idea why?
(defun clear-shell-buffer ()
"Clear the contents of the current buffer"
(interactive)
(erase-buffer)
;; (insert "/usr/games/fortune -a")
(comint-send-input)
)
(put 'erase-buffer 'disabled nil)
(eval-after-load 'shell
'(define-key shell-mode-map [(\C-z)] 'clear-shell-buffer))
This is happening because of the key binding being incorrect. You can verify this by doing C-h k C-z when in shell mode.
Instead of [(\C-z)], use one of these options:
[(?\C-z)]
[(control ?z)]
(kbd "C-z")
which will correctly set the key binding, and let you call the correct function with C-z
You can see a little bit of what's happening if you evaluate just the those statements. Here's the output I get for each
(define-key shell-mode-map [(\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(C-z)] (quote clear-shell-buffer))
(define-key shell-mode-map [(?\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(26)] (quote clear-shell-buffer))
You can see that the types differ for the key binding. Right now, you're passing a symbol, when you want to be passing a character code.

Why doesn't my keymap for C-c C-c work in C++?

I'm new to emacs and using emacs 24 and trying to bind C-c C-c to a function to comment out a single line. I have the following in my init.el file but it doesn't seem to work in c++.
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(comment-or-uncomment-region (line-beginning-position) (line-end-position))
(next-line))
(global-set-key (kbd "C-c C-c") 'toggle-comment-on-line)
When I'm playing around in the scratch page it works fine and when I check with C-h k C-c C-cit displays the right function but when I'm in C++ the same command displays the text:
C-c C-c runs the command comment-region, which is an interactive
compiled Lisp function in `newcomment.el'.
It is bound to C-c C-c, <menu-bar> <C++> <Comment Out Region>.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from `comment-start'
and `comment-padding'; the strings used as comment ends are built
from `comment-end' and `comment-padding'.
By default, the `comment-start' markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with `comment-style'.
I assume something else is overriding C++ keybindings but I don't know what or how to fix it? Does anyone have any ideas?
Yes, c++ mode has its own keymap, which overrides the global map. Use the following instead:
(define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line)
I've improved your code a bit, and below there's also the
code to bind the key without an error
(it happens because you're trying to define a key in
c++-mode-map before it was defined)
(defun toggle-comment-on-line ()
"comment or uncomment current line"
(interactive)
(let ((beg (if (region-active-p)
(region-beginning)
(line-beginning-position)))
(end (if (region-active-p)
(region-end)
(line-end-position))))
(comment-or-uncomment-region beg end)
(next-line)))
(add-hook 'c++-mode-hook
(lambda()
(define-key c++-mode-map (kbd "C-c C-c") 'moo-complete)))
As a side note, I strongly recommend against binding C-c C-c,
as this is a very popular mode specific binding that's different in every mode,
but means generally confirm:
in org-mode it evaluates a babel block of code
in message-mode it sends the email
in python-mode it sends the buffer to the process
in wdired it confirms your edits to the file names
So you'll really have a headache if you bind it, unless you're
using Emacs just for c++-mode.
I've been using Emacs for 3 years now and I have comment-dwim
on C-.. I'm quite happy with it so far.
If you're willing to use a different key binding, you can use the following code:
;; Nothing to see here.
after which you can do C-a C-SPC C-n M-;.

Reloading AucTex Labels and Defining Keybindings

When I add a label in emacs to a .tex file, I used to reload the file to get it to show up in RefTeX. i.e. C-c ) wouldn't have the new label unless I reloaded the file.
After some searching I found that C-u C-c ) will refresh RefTeX before trying to do the reference. This works as I would like, but I would like to use C-c r for this command instead of typing C-u C-c ) every time. How do I do this?
Thanks,
Jim
I don't use reftex but as far as I can understand you want just to define a binding:
(define-key reftex-mode-map (kbd "C-c r") 'reftex-reference)
Building on Oleg's answer:
Maybe it would be better to feed it the C-u argument already if that's what you are after:
(defun call-reftex-reference-directly ()
(interactive)
(let ((current-prefix-arg 4)) ;; emulate C-u
(call-interactively 'reftex-reference) ;; invoke reftex-reference
)
)
(define-key reftex-mode-map (kbd "C-c r") 'call-reftex-reference-directly)
Maybe try if this works for you?
You can type r in the label selection buffer to refresh it without reloading the file. The refresh is instantaneous (unless you have very large/very many linked files).

Emacs: Keyboard shortcut to run ansi-term with a specific shell

I would like to associate a keyboard binding in Emacs (e.g. C-c a) that automatically starts an ansi-term window with a shell that I have pre-specified in my .emacs file (without prompting anything)
For reference, there are two threads in StackOverflow that address similar problems:
Remote ssh connection from within Emacs
Emacs: Default binary to run with M-x ansi-term
but it isn't obvious to me how to combine the ideas in those threads to get an answer to my question.
(global-set-key (kbd "C-c a") '(lambda () (interactive) (ansi-term "/bin/zsh")))
I suggest you to use multi-term. As its name implies, it lets you deal with multiple term using ansi-term.
Here is a small configuration:
(require 'multi-term)
(eval-after-load "multi-term"
'(setq multi-term-program "/bin/bash"
term-unbind-key-list '("C-x"
"C-h"
"M-x"
"C-z")
term-term-name "xterm-256color"))
(global-set-key (kbd "C-c a") 'multi-term-next)
My whole configuration for multi-term is
here
(compilation-shell-minor-mode is really nice).

emacs - set shortcut key only in major mode?

I would like to override C-l and use it to do M-x erase-buffer followed by simulating hitting RET, only when I am in m-shell-mode. C-l should be its default, recenter-top-bottom, otherwise. How do I do so?
Not sure what m-shell-mode is, but if it's a well-defined major mode, then the following should do the trick:
(require 'm-shell-mode)
(define-key m-shell-mode-map (kbd "C-l") 'erase-buffer)
Might I suggest an alternative binding, which has the same visual effect, but keeps the buffer contents around (which can be handy).
(defun shell-clear-command (&optional a)
"\"clear\" the screen"
(interactive "P")
(recenter (or a 0)))
(define-key m-shell-mode-map (kbd "C-l") 'shell-clear-command)
If m-shell-mode is based on comint-mode, which is true of many modes that provide a shell to interact with another process, then you can pass the return keypress to matlab with the function comint-send-input. In that case the following code should do what you want:
(defun clear-and-return ()
"Erases the buffer, and then passes a return to the buffer process.
Assumes the buffer is attached to a comint process."
(interactive)
(erase-buffer)
(comint-send-input))
(defun my-m-shell-mode-hook ()
(local-set-key (kbd "C-l") 'clear-and-return))
(add-hook 'm-shell-mode-hook 'my-m-shell-mode-hook)
The first defun makes a function that does what you want. The second is a hook function that will bind C-l to that function for the buffer that is active when the function is called. The add-hook tells emacs to run the second function whenever you start m-shell-mode. You can add further m-shell-mode customizations inside the body of my-m-shell-mode, and Emacs will run all of them each time you start the mode.
If m-shell-mode is not based on comint-mode, you need to find out what happens when you press return. From a buffer that is running the mode, type C-h k RET to find the function bound to the return key. Use that function instead of comint-send-input in the code above.
You can add to your m-shell-mode hook the following code:
(local-set-key (kbd "C-l") 'erase-buffer)