Use Ctrl-X commands while in terminal in emacs? - emacs

When I'm running a terminal inside emacs (with M-x term) I can't seem to use commands that start with C-X, such as, say C-x o to switch panes or C-x C-c to exit. Instead it seems that the terminal itself is receiving these C-x signals. By contrast, C-c commands are received by emacs itself. How can I change this behavior?

term has two different input submodes. In the default (character) mode, C-x simply transmits a literal control x to the terminal. Many keybindings which are normally available in the C-x map are instead now in the C-c map, so you can switch to a different buffer in the other window with C-c 4 b. Or you can switch to line mode with C-c C-j (and back to character mode with C-c C-k).
See also the documentation.

Related

Check all the commands prefix with C-c

I desire to review all the key-sequences with prefix "C-c"
Issue C-c, it prompts
and remind that C-h C-n to reference the next page.
I want to view them all simultaneously within a single buffer and tried to C-x 1 with intention of see them in a full-screen buffer.
Unfortunately, such an operation is deactivated in the mini-buffer which C-c invoke, In contrast, grep-find, grep's minibuffer could be manipulated with C-x 1.
How could view all the commands prefix with C-c
You should be able to use C-c? to see all the commands prefixed by C-c. If, by chance, C-c ? is bound already, try C-cC-h

How to get GHCi running correctly within ansi-term window in Emacs?

For some reason, I'm loosing the up-arrow and DEL keys after launching GHCi from within an ansi-term window in Emacs.
(These keys work as expected from within the ansi-term window, before launching GHCi.)
And RET doesn't produce a line feed, just a carriage return.
I don't observe any of these oddities when launching GHCi from my normal Terminal application under MacOS X.
Using Emac's view-lossage feature I find:
<menu-bar> <Terminal> <Character mode> [term-char-mode]
<up> [term-send-up]
<return> [term-send-raw]
<backspace> [term-send-backspace]
<menu-bar> <Terminal> <Line mode> [term-line-mode]
<up> [previous-line]
<return> [term-send-input]
<backspace> [delete-backward-char]
C-h l [view-lossage]
(Note: The above was taken after launching GHCi from within the ansi-term window of Emacs.)
And it looks like I may want to switch to Line mode while running GHCi.
(I normally run the ansi-term window in Character mode, because it gives me an experience more similar to working in the Terminal application.)
It looks like M-p will recover past commands.
So, if I can figure out how to get the up-arrow remapped to M-p while in GHCi, then I should be pretty close to my usual GHCi-in-Terminal experience.
However, there's one remaining problem: I'm still not getting a line feed when hitting RET.
And this is really screwing things up, both asthetically and functionally.
It's strange, because the RET key typed at either the ansi-term prompt, or the GHCi (launched from within ansi-term) prompt (with the ansi-term window set to Line mode) both send the same command: term-send-input.
And the ansi-term does the right thing in response to this command.
So, why doesn't GHCi do the right thing?
Perhaps, I need to remap RET to: CR/LF; term-send-input while in GHCi?
Is this even possible (i.e. - doing application-specific key remapping withing the ansi-term window)?

How to bind C-x SPC locally to gud mode in Emacs 25

Similar to How to change GUD breakpoint keybinding to the old one but I would like to bind CTRL+x followed by SPACE to be gud-break.
What I have working (well it is a hack; keep reading) is:
(define-key ctl-x-map " " 'gud-break)
but ctl-x-map is a global variable akin to the global map. And in fact, if I switch to another C++ buffer and type C-h k C-x SPC I get:
C-x SPC runs the command gud-break (found in global-map), which is an interactive compiled Lisp function.
It is bound to C-x SPC, C-x C-a C-b.
(gud-break ARG)
Set breakpoint at current line.
which means the global definition across all buffers is what was changed, which is not correct AFAIK.
Is there a way to "insert" or somehow affect the local key map for C-x, which I believe is gud-mode-map, because I want that binding to not be global for all C++ buffers. E.g., the global binding for CTRL+x followed by SPACE is rectangle-mark-mode.
Yes I realize the standard binding is C-x C-a C-b for gud-break, but that is asking for RSI.
Update #1
Since I need the local key map for gud-mode-map, this needs to be active only when I'm running a debugger. In my case, this is in C++ mode buffers, but my understanding is that gud-mode-map becomes active in those C++ mode buffers only during the debug session, and is removed from the key bindings when gud mode is finished.
Update #2
This did not work:
(define-key gud-mode-map [(control x ?\ )] 'gud-break) ;; <-- gave "Two bases given in one event" error too.
(define-key gud-mode-map (kbd "C-x SPC") 'gud-break) ;; <-- this does not work either.
Update #3
As an experiment, I commented out my define-key bindings in my hook I add to gud-gdb-mode-hook, reran gdb, then switched to the gud buffer (not the C++ source file) and typed C-h k C-x SPC I get this:
C-x SPC runs the command gud-break (found in gud-mode-map), which is
an interactive Lisp closure.
It is bound to <menu-bar> <debug> <break>, C-x SPC, C-c C-b, C-x C-a
C-b.
(gud-break ARG)
Set breakpoint at current line.
But then when I switch over to the C++ buffer that should also have the same bindings inserted temporarily (while gud mode is active), and then do the same thing I get this instead:
C-x SPC runs the command rectangle-mark-mode (found in global-map),
which is an interactive autoloaded compiled Lisp function in
'rect.el'.
It is bound to C-x SPC.
(rectangle-mark-mode &optional ARG)
Toggle the region as rectangular.
Activates the region if needed. Only lasts until the region is deactivated.
Switching back to the gud buffer, and typing C-h m shows this:
Debugger mode defined in 'gud.el':
Major mode for interacting with an inferior debugger process.
You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
hook; 'gdb-mode-hook', 'sdb-mode-hook', 'dbx-mode-hook',
'perldb-mode-hook', 'xdb-mode-hook', or 'jdb-mode-hook' respectively.
After startup, the following commands are available in both the GUD
interaction buffer and any source buffer GUD visits due to a breakpoint stop
or step operation:
C-x SPC sets a breakpoint at the current file and line. In the
GUD buffer, the current file and line are those of the last breakpoint or
step. In a source buffer, they are the buffer's file and current line.
...
Notice the reference to C-x SPC above. It is as if they intended to bind C-x SPC but it did not work, or something is preventing it from being bound properly in that buffer when gud mode is entered.
I'm not exactly clear on what you want. But if you only want to redefine the key C-x SPC when the gud-mode-map is active, then tell define-key you want to use that map: (define-key gud-mode-map ...).
I've chosen to give up on this altogether. using C-x SPC in the buffer is problematic anyhow. I noticed that the C-x C-a prefix that is used for the gud bindings are left in the C++ buffer even after gud finishes, so the gud mode is not cleaning up after itself anyhow.

emacs - Kill the buffer with terminal emulator

I am aware of this option:
Kill the *terminal* buffer with C-d
But I am still a newbie in emacs, what is the original method to do this? I tried C-x k but it doesn't like it, C-x C-c kills all my windows and buffers...
I assume you're using a buffer in term-mode (M-x term). term-mode is a bit different than most modes in that it has two submodes, char and line. In char mode it acts more like a terminal than emacs, as in a lot of normal keybinds are not available. Put it in line mode with C-c C-j to put it in line mode, then you can run most normal keybinds including C-x k. You can put it back into char mode with C-c C-k.
I use the multi-term package, which I think has better defaults than plain term-mode and as the name suggests makes it easy to have many terminal buffers.

Emacs C-h c doesn't seem to work for chords 3 combinations long?

I'm trying to use C-h c in emacs to figure out what a key combination is bound to. The combination is C-u C-c C-q, which realigns tags in org-mode. However, Emacs just tries to look up C-u C-c and then fails. What am I doing wrong? I realize I could easily look at the orgmode source or something to figure this out, but for future reference what would I do to figure out what function something like this is bound to?
Edit: OK, so it's actually C-u followed by C-c C-q, and according to emacs this is what that combination is bound to:
(org-set-tags-command &optional arg just-align)
Call the set-tags command for the current entry.
So what exactly does it mean to give this command the argument 4?
Oh, just to give an explanation: I'm trying to start learning emacs-lisp and customization and one of the things I wanted to do was to have this command added to the before-save-hook so that when I save an org file, the tags get automatically aligned.
Final edit: I figured out why this command behaves as it does; given the prefix argument it changes its behavior. How can I set the prefix argument when calling the function in elisp?
It's not a general problem with combinations that are three keys long: For example, C-h c ESC ESC ESC (keyboard-escape-quit) or C-h c C-x r t (string-rectangle) both work fine.
When I try C-h c C-u C-c C-q in org-mode, the command interrupts after C-u and shows:
C-u runs the command universal-argument
in the minibuffer, which is correct. So, in fact, "C-u C-c C-q" is not a command, it's the command "C-c C-q" (org-table-wrap-region) started with an additional argument (4 -- see C-h k C-u for an explanation).