I recently switched from vi to emacs. I like CUA mode. However, CUA mode makes C-x kill the active region. This is annoying when I want to, say, clear a rectangular selection with C-x r c.
How can I use CUA mode while having completely out-of-the-box behavior for C-x?
If you look at the help for cua-mode it gives you options for using commands which conflict with the prefix keys (like C-x):
If you really need to perform a command which starts with one of
the prefix keys even when the region is active, you have three
options:
- press the prefix key twice very quickly (within 0.2 seconds),
- press the prefix key and the following key within 0.2 seconds, or
- use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
Use cua-selection-mode instead of cua-mode.
The former provides CUA's other configuration and facilities (for example the rectangle editing mentioned by Francesco), but it leaves C-x, C-c, C-v, and C-z alone, meaning that it's highly compatible with out-of-the-box Emacs usage.
In addition to jtahlborn's general answer, please also note that cua-mode reimplements rectangular commands (IMHO in a much fancier way than the standard C-xr commands)
Just press C-RET to enter rectangular selection mode. From there regular commands work on the rectangular selection: you can for example delete the region using DEL.
Related
In Haskell-mode, the shortcut C-c C-= is defined to do something.
Trying this shortcut, I realized that emacs do not recognize the shortcut C-c C-=.
Indeed, when I try the shortcut on emacs, the buffer write C-c = is not defined although I pressed C- C-=. I have the same problem with some other symbols like '.' or '§'. But shortcuts like C-c C-l or C-c C-c work.
I try to remove my .emacs but I have the same problem.
a friend have the same problem as me.
Both we are on ArchLinux (64 bits) and we use emacs in console. The keyboard is an azerty.
The problem come from emacs ? Arch Linux ?
Your terminal can't send Emacs C-= so you can't use that key sequence. (Emacs would recognise it if it received it, but that won't happen.)
Your options are:
Run GUI Emacs.
Use M-x name-of-command RET (for whatever command is bound to the key sequence you're not able to use). Use C-hm to see the major mode's bindings, or C-hb to see all current bindings, in order to learn what those command names are.
Create new custom keybindings for the commands in question (i.e. bindings which your terminal can send to Emacs).
Find a different terminal emulator with enhanced key sequence abilities. The vast majority of them will be no better than what you have, because they're all adhering to the limitations of the terminals they're emulating. The most capable one I know of is http://invisible-island.net/xterm/xterm.html but you may need to compile it yourself, and then expect to spend lots of time configuring it. (It's not a trivial solution, though, and xterm requires a GUI environment, so running GUI Emacs is much simpler.)
Use C-x#c<key> instead of C-<key>.
With that last option, you can use a sequence your terminal can send to fake a sequence that it can't send.
C-cC-= would become C-cC-x#c=
If you really wanted to use that last option, you can set a custom binding to simplify the sequence (may be necessary in some instances to avoid conflicting with existing sequences). See the end of https://stackoverflow.com/a/24804434/324105 for more information.
To add to phils' post - another option is an Emacs package (which I wrote), which can teach Emacs and terminals how to properly recognize all PC keyboard keys and modifier key combinations:
https://github.com/CyberShadow/term-keys
The default configuration already includes encoding key combinations like Ctrl=, as well as similar variations.
My problem is that I want to bind Control + Backspace combination to backward-delete-word function. However, it doesn't seem possible because emacs doesn't detect the Control key in combination with backspace.
I detected that by looking at the "logger" (C-h l). So if I press Backspace, it shows DEL. If I press Control + Backspace, it still shows DEL.
Is there any way to achieve that?
I have read and tried other questions, but couldn't fix it.
As #phils pointed out, if you are using Emacs in a terminal (e.g. emacs -nw) instead of as a GUI (no -nw command-line option), and if you are not on MS Windows, then by default certain key sequences are not available to you.
However, it might still be possible for you to configure the terminal (e.g., xterm) so that it in fact supports some such key sequences for Emacs. See this comment by the Emacs maintainer in the discussion of Emacs bug #10387.
(That said, you might find it simpler to just use a different key sequence. Or switch to the GUI version of Emacs.)
Is there a key binding to quit a mode and return to the previous mode in emacs?
For example suppose I entered line number mode using the following command:
Alt+x linum-mode
How can I quickly disable this mode and return to the mode which I was in before (not using the same command again)?
Why wouldn't you want to use the same command again?
M-xM-pRET
It doesn't get much simpler than that.
Edit: You can repeat M-p in that sequence to step back further in the command history, and you can search the command history with M-xC-r.
Also, when you disable a minor mode you're not "returning to the mode you were in before"; you're just disabling one (of many) minor modes which are all active at the same time.
Tangentially, the concept of "returning to the mode you were in before" could apply to major modes (as there's only ever one active major mode in a given buffer), but strictly speaking there's no notion of 'disabling' a major mode -- only of 'enabling' the one you wish to change to -- so to 'toggle' between two major modes, you would need to call them alternately.
Just repeat the same command: M-x linum-mode. Such minor-mode commands are toggles: on/off.
C-x z calls repeat - repeat last command.
Repeatedly calling a minor-mode enables/disables it.
One more way to do it is with smex: the last command
that you called with M-x kinda sticks around.
So you can enable linum-mode with smex, do a bunch of editing
with usual shortcuts and then disable linum-mode with
M-x RET.
A solution might follow the path kill-ring-save works: store the modes being active as current-modes-listing in a previous-modes-ring.
The code needed therefor exists basically inside describe-mode, see upward from "Enabled minor modes" - respective for the major mode.
Then a hook should check if this-command has "-mode" in it's name. If yes, check, if current modes-listing equals car of previous-modes-ring. If not, add new setting.
Finally write a command which sets current modes according to selected listing from previous-modes-ring.
In emacs (ver. 24.3), I have my forward-paragraph and backward-paragraph mapped to M-p and M-n instead of M-{ and M-}. It is easier for me to remember and use fluidly with C-p and C-n. I've recently started using multi-term to run most of my terminal work. However, when I switch to line-mode my custom bindings for forward and backward paragraph no longer work. It says 'empty input ring'. Oddly when I'm in char-mode, the C-p and C-n do what they are supposed to do (bringing up previous prompt entries), but my paragraph movements work.
So in short, my custom forward and backward paragraph bindings work in char-mode (where I don't really need them), but not in line-mode. Any ideas?
See term-bind-key-alist, which includes C-p, C-n, M-p, and M-n by default. See also these passages from the EmacsWiki page MultiTerm. The second especially seems relevant to your problem. These do not mention term-line-mode or term-char-mode, but I think they might give you a place to start.
Note 1
‘term-unbind-key-list’ is a list of keys which emacs keeps for itself. By default it contains (“C-z” “C-c” “C-x” “C-h” “C-y” “”)
‘term-bind-key-alist’ is a list of keys and functions which you can use, for example to use Emacs style cursor movement to the multi-terminal. The default is long, so I’ll let you look it up yourself.
Note 2
Because C-r is default keystroke for isearch-backward, for avoid conflict with C-r, i binding M-r to send “C-r” character to shell.
You can use option term-bind-key-alist/term-unbind-key-list to binding/unbinding special keystroke in multi-term.el, and don’t use term-mode-hook. ☺ –- AndyStewart
Also, I don't see term-line-mode anywhere in multi-term.el. It looks as if it makes use only of term-char-mode. See, for instance, multi-term-keystroke-setup.
I want to select or highlight a block in Emacs without using the mouse, but doing it from the keyboard like Vim's visual mode. What is the easiest way to do this from a keyboard?
If I understand the question correctly, it is not about rectangular regions originally.
C-Spc puts a mark at the current position.
Wherever your cursor is afterwards, the text between the last mark and the current position is "selected" (you can highlight this by activating transient-mark-mode, but this will also mean that marks have to be deleted when you don't want highlight).
You can operate on that region with commands like:
C-w . . Kill region. This deletes and puts the region into the kill ring.
C-y . . Yank. This inserts the last snippet from the kill ring.
M-y . . Cycle kill ring. Immediately after C-y, this replaces the yanked part by the other snippets in the kill ring.
M-w . . Save region into kill ring. Like C-w, but doesn't delete.
This is just the basic usage. Marks have other uses, too. I recommend the tutorial (C-h t).
Take a look at region-rectangle in Emacs.
In short, you start selection like usual with Control-Space, then kill region with Control-x r k and paste (or yank) killed block with Control-x r y.
Emacs 24.4 now has rectangle-mark-mode. Use Ctrl + X, Space to invoke it.
Although C-SPC is a common way to start marking something from
wherever your point is, there are often quicker/easier ways that don't
involve explicitly moving to start/end points...
Built-in selection shortcuts
M-h — an important means to mark a paragraph. A "paragraph"
often means a block of code.
C-M-h and C-M-# — for marking sexps and defuns,
respectively. This works for several languages, not just lisps.
hold down shift — another slick way to highlight during
movement. E.g., M-S-f selects forward a whole word. This is
shift-select-mode,
and it is enabled by default in Emacs 24+. On some (non-chiclet)
keyboards, you should be able to hold down C-S- with a single
pinky.
You can press any of these repeatedly to grow the selection.
There are also a few special ways to mark things:
C-x h — mark the whole buffer
C-x SPC — enter rectangle mark mode
(NOTE: use C-g often to cancel marking while
experimenting.)
Add-ons
There are a few add-on packages that improve selecting regions and
things. These are all play nicely together and fit different use
cases. Use them all!
expand-region:
Expand region increases the selected region by semantic units. Just
keep pressing the key until it selects what you want. C-= is a
recommended binding for it. Hit it a few times to get what you
need.
easy-kill: Use M-w and
a mnemonic to select different types of things, like words, sexps,
lists, etc.
zop-to-char:
Like zap-to-char, but provides nice selection and other
menu-driven actions.
diff-hl: Highlight uncommitted changed regions. Use diff-hl-mark-hunk to select/mark a hunk.
symbol-overlay: Select symbol at point with a keystroke (M-i). Then you can do other things with it, like copy, search, jump, replace, etc.
Use Control-Space to set a mark and move your cursor.
The transient-mark-mode will highlight selections for you. M-x transient-mark-mode.
You can setup Emacs to enable this mode by default using a customization. M-x customize-option RET transient-mark-mode.
... and in case you are using Ubuntu and Ctrl + space is not working for you: you need to clear the Intelligent Input Bus (IBus) "next input method" key binding, as in
run ibus-setup and change the key binding for
"next input method" to something else (or delete it entirely by
clicking the "..." button and then the "Delete" button).
The quote is taken from an answer to a Stack Overflow question.
To expand answer of Edin Salkovic, if you use CUA mode, you can use Ctrl + Enter to begin a visual block selection. There are plenty of shortcuts to control block selection described in the documentation of CUA.
With Emacs 25, simply press Ctrl + Space and then move your cursor wherever you want to highlight/select the region of text which interests you. After that, you may need these commands:
Ctrl + W for cutting.
Alt + W for copying.
Ctrl + Y for pasting.