How to send every command to Emacs Term? - emacs

Is it possible to configure emacs term to send everything (maybe exception M-x) as raw commands. This will allow for instance run emacs -nw inside terminal and every command will work for emacs inside terminal now the one outside.
I want something like this because I sometimes run nano from terminal or screen, I also use ssh and this will alow me to run emacs on the server. Right now when I run nano I need to call C-c x that send C-x.

I'd first suggest using tramp to edit remote files. I prefer it to opening an editor on the remote machine. If you try to run emacs inside a term-mode buffer, you're going to be fighting it all the time.
If you must run emacs inside a term-mode buffer, you can use term-send-raw and term-send-raw-string. For example:
(defun term-send-backward-word ()
"Move backward word in term mode."
(interactive)
(term-send-raw-string "\eb"))
<Escape> b is what the terminal (which is eterm-color) expects when you press C-<left>. This is not necessarily the same as binding C-<left> to term-send-raw. The best thing to do is probably to try binding whatever key to term-send-raw, and if that doesn't work, make a function with term-send-raw-string and bind that. You can figure out what the string should be if you have a shell in the term-mode buffer, send a quote, and then type the key. You can send a quote with
(defun term-send-quote ()
"Quote the next character in term-mode.
Similar to how `quoted-insert' works in a regular buffer."
(interactive)
(term-send-raw-string "\C-v"))
It's just like typing C-v in a normal terminal.
Finally, I'll mention multi-term. It's available in melpa. It provides the functions I listed above, and has better defaults than term-mode IMO. But it's probably further from what you want, because it tries to integrate term-mode with the rest of emacs instead of just passing things through.

Related

Why does Emacs local-set-key not overwrite a globally set key in mode hook?

In Emacs' tide-mode (typescript development) I would like to use M-q, which is normally bound to fill-paragraph, to rather run tide-format. I have a mode hook like
(defun setup-tide-mode ()
...
(local-set-key [M-q] 'tide-format)
(describe-key [M-q]))
(add-hook 'typescript-mode-hook #'setup-tide-mode)
When I open a typescript file I do see the *Help* buffer which indeed shows
<M-q> runs the command tide-format ...
Yet when I then run C-h k M-q to describe the key binding of M-q, I get
M-q runs the command fill-paragraph
There is this suspicious difference in the printout between <M-q> and M-q. This is probably telling me something, but I don't know what.
What would be the correct way to locally overwrite M-q to run a different command?
You want to use [?\M-q] instead of [M-q] because ?\M-q is the event generated when you press the Alt/Meta modifier along with the Q key.

How to use terminal keyboard shortcuts inside emacs multi-term

I know there's some questions that are kind of related to this already, and I know you can do stuff like
(defun term-send-esc ()
"Send ESC in term mode."
(interactive)
(term-send-raw-string "\e"))
but it would be very convenient if there was kind of a univerisal override keybinding. For example, I just ssh:ed into a remote server and tried to nano a file and couldn't figure out how to exit because Ctl-x listens for emacs bindings. Is there such a thing?
There are two term sub modes, term-char-mode (C-x C-k) and term-line-mode (C-x C-j). Char mode is closer to a terminal, line mode is closer to a regular buffer. IIRC, multiterm starts in char mode, but it rebinds some keys to make it more Emacs-like. Look at the keys that multiterm rebinds; it has some alists that are used to modify the existing term-mode keymaps instead of using its own keymap. Or use sane-term, which is not much more than some commands to create new term buffers (no mucking with key bindings).
Ctl-x listens for emacs bindings.
Not in the default term-char-mode it doesn't, so your problem is most likely with your own config.
Run a terminal in emacs -Q to confirm the standard behaviour.

Use semicolon in global-set-key for function in .emacs

I am trying to set [Ctrl]-[;] as a keybinding in my .emacs like this
(global-set-key "\C-;" 'comment-or-uncomment-region)
however it doesn't work when I try (i don't get any error messages, it just has no effect). It will work though if i try a normal character (such as setting it to "\C-p").
I have also tried
(global-set-key (kbd "C-;") 'comment-or-uncomment-region)
but I don't like this option because for me it doesn't work when i run "emacs -nw". Any thoughts on how I can do this?
EDIT:
When I run C-hcC-; in emacs -nw I get the output:
; runs the command self-insert-command
which is exactly the same as when I run C-hc; in emacs -nw
So I believe phils is right, that it is a terminal problem, because emacs never actually sees C-;, it only sees ;
Indeed C-; is typically not something your terminal is able to send to an underlying application like Emacs (so it works under a GUI but not in a terminal). But I wonder: why do you need such a binding anyway, given that M-; is already bound to comment-dwim which does comment/uncomment the region when the region is selected, so it provides a superset of comment-or-uncomment-region.
Using (kbd "C-;") is absolutely fine and correct.
I suspect when you type C-; when running emacs -nw, your terminal is not actually sending anything to Emacs.
So your problem is more likely to be a question of how to get your terminal to send C-; to Emacs (or alternatively how to get Emacs to recognise the sequence which is sent).
If you run emacs -Q -nw and type C-hcC-;, do you get a "C-; is undefined" message?
Assuming that it is a terminal issue, here are some related Q&As which may point you in the right direction, but it's going to depend upon the particular terminal you are using.
Binding M-<up> / M-<down> in Emacs 23.1.1
Send "C-(" to Emacs in VT100/xterm terminal (Mac OS X's Terminal)?
How does one send S-RET to Emacs in a terminal?
emacs -nw issues with cscope and terminals

Yanking text into a terminal running in Emacs

I am unable to yank text into a terminal running in Emacs.
This is my procedure:
I killed the string "date" from one buffer and yanked it into the terminal in another buffer and hit return.
The terminal behaves as if I typed nothing. It just returns the prompt back.
I am using OS X 10.5.8 and Emacs 23.1. I have tried this procedure on Aquamacs, Carbon Emacs, and the release from http://emacsformacosx.com/. They all show this weird behaviour even in their default configurations with my .emacs file empty. What could possibly be causing this?
By "in a terminal" I assume you mean you're running Emacs's built-in terminal emulator. Ordinarily, the terminal emulator transmits most keys exactly as typed to the shell process. Type C-c C-j in the terminal buffer to put it into a state where ordinary Emacs key bindings are available. You'll see the mode line change from (Term: char run) to (Term: line run).
Addendum:
Yanking text without leaving char mode is a little tricky; the relevant function, however, is term-paste (not yank, which merely inserts the text into the terminal buffer without sending it to the inferior process). term-paste will immediately send the most recent kill to the inferior process, but doesn't provide the fancy yank functionality you're probably used to (like M-y to cycle through prior kills). You could run term-paste as an extended command: C-c M-x term-paste RET.
Probably the easiest solution is just to temporarily go into line mode (C-c C-j) when you have something to paste, and then immediately go back into char mode (C-c C-k). Or even easier, just stay in line mode all the time. I often do this when I have a terminal logged into an Oracle SQL*Plus session. I rarely notice the difference, but I get all sorts of convenient Emacs functionality, like being able to type M-p to cycle through a long, previously-typed SQL statement.
I would have assumed that you could always start off in line mode like this:
(add-hook 'term-mode-hook 'term-line-mode)
...but it doesn't work for me. Don't know why.
In the buffer with the terminal running, put the terminal into line mode with C-c C-j. To paste in your text, now press S-Insert (that's Shift-Insert). If you need the terminal to go back to char mode afterwards, it's C-c C-k.
When all else fails I just highlight the text and click Edit->Copy then right click in the other emacs buffer and click paste.

Is there a (repeat-last-command) in Emacs?

Frequently, I've dug into apropos and docs looking for something like the following only to give up to get back to the task at hand:
(repeat-last-command)
do the last C- or M- command I just executed (to be rebound to a fn key)
or sometimes the related:
(describe-last-function)
what keystroke did I just mistakenly issue, the effect of which I'd like to add to my bag of tricks. describe-key is close, but requires knowing what I typed.
Am I simply asking too much from my trusty sidekick?
Repeat functionality is provided by the repeat.el Emacs Lisp package, which is included with standard Emacs distributions. From repeat.el's documentation:
This package defines a command that
repeats the preceding command,
whatever that was, including its
arguments, whatever they were. This
command is connected to the key C-x z.
To repeat the previous command once,
type C-x z. To repeat it a second time
immediately after, type just z. By
typing z again and again, you can
repeat the command over and over.
To see additional information about the repeat command, type C-h F repeat RET from within Emacs.
Repeat last command
C-xz
Once you pressed it, just press only
z
after that and it will repeat (without having to press C-x again).
Yes, there is a repeat command. It's called repeat:
You can repeat commands with C-x z, and hit z to keep repeating.
A bit shocking nobody mentioned repeat-complex-command, available from the key binding C-x ESC ESC.
with regards to 'describe-last-function':
There's a variable last-command which is set to a symbol representative of the last thing you did. So this elisp snippet - (describe-function last-command) - ought to bring up the documentation for the thing that immediately happened.
So you could make a trivial working describe-last-function like so
(defun describe-last-function()
(interactive)
(describe-function last-command))
Put that elisp in .emacs or equivalent, and you'll have a M-x describe-last-function.
If you've banged on a few keys or done something that modified last-command since the thing you're interested in, the command-history function might be of interest. You can get that by M-x command-history
Also, M-x view-lossage shows you the last hundred(?) keystrokes you entered. So, you'll be able to see where the command is. It's what i used until i just right now found out about M-x command-history which i think i'll be using with C-h w now.
I'm not really sure, but maybe you are searching for this one?
The command C-xz (repeat) provides another way to repeat an
Emacs command many times. This command repeats the previous Emacs
command, whatever that was. Repeating a command uses the same arguments
that were used before; it does not read new arguments each time.
Emacs Manual, 8.11 Repeating a Command
May be this would help too...
From emacs Help verbatim:
C-x M-ESC runs the command repeat-complex-command
which is an interactive compiled Lisp function in `simple.el'.
It is bound to <again>, <redo>, C-x M-:, C-x M-ESC.
(repeat-complex-command ARG)
Edit and re-evaluate last complex command, or ARGth from last.
A complex command is one which used the minibuffer.
The command is placed in the minibuffer as a Lisp form for editing.
The result is executed, repeating the command as changed.
If the command has been changed or is not the most recent previous command
it is added to the front of the command history.
You can use the minibuffer history commands M-n and M-p
to get different commands to edit and resubmit.
Personally I found Sebastian's idea useful. Here is a working version
(global-set-key "\C-r" #'(lambda () (interactive)
(eval (car command-history))))
This is old, but Google pops post this up first when I was looking to retrieve the last command I typed at the Emacs prompt. None of these answers worked for me so I decided to put in my two cents for those who might stumble upon this later on as I did. I'm using Portacle, but I found what I was looking for in here so I'm hoping it's generic enough to work with different setups. Anyway, what worked for me is using C-&uparrow; and C-&downarrow; to cycle through the history. Using M-p and M-n worked as well, but I prefer using the arrows since I use Bash quite a bit.
dot-mode is a way to repeat the last command(s).
From its commentary:
It emulates the vi `redo' command, repeating the
immediately preceding sequence of commands. This is done by
recording input commands which change the buffer, i.e. not motion
commands.