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.
Related
In EMACS the default seems to be to 'copy' any text highlighted with the mouse. I'm attempting to replicate the functionality of modern text editors, where I can highlight a section of text and press 'paste' to replace it. I have so far added
(delete-selection-mode 1)
to my init.el
The problem is, if I copy something, then highlight to paste in its place, I end up pasting what I had just highlighted, changing nothing.
What do I need to change to fix that behaviour?
The most powerful element of emacs is its introspection features, lets have a look at how we can use them to try and solve this problem. We must use the power of the source.
One of the most important tools for introspection in emacs is the describe-key command which is bound to C-h k. It brings up the documentation of whatever keystroke is called after it. So in our case if we press C-h k and then click and drag we will see the documentation for <down-mouse-1> and more importantly for <drag-mouse-1>. The documentation states that "<drag-mouse-1> at that spot runs the command mouse-set-region". Below it then gives some documentation for this command. It says
Set the region to the text dragged over, and copy to kill ring.
This should be bound to a mouse drag event.
See the ‘mouse-drag-copy-region’ variable to control whether this
command alters the kill ring or not.
Now we know that somehow mouse-drag-copy-region controls whether or not the highlighted text is copied.
If we follow the link of that variable it tells us the default value and some documentation:
If non-nil, copy to kill-ring upon mouse adjustments of the region.
Now all we have to do is set the variable to be nil to get the effect that you want. Place the following code at the end of your init file and you should be all set
(setq mouse-drag-copy-region nil)
I hope that this helps you with this problem and that more importantly it helps you with further issues.
By default, selecting a region with the mouse does not copy the text to the kill ring. If your Emacs does this, you probably have set the variable mouse-drag-copy-region.
In a fresh Emacs (24.5 started using -Q), you can do the following:
Start delete-selection-mode.
Mark a region using the mouse. Copy it using M-w.
Mark a second region. Replace it with the first using C-y.
I see two alternatives, neither of which does exactly what you request. (For both, yes, turn on delete-selection-mode.)
Use the secondary selection for the text to copy, and use the primary selection (the region) for the text to be replaced.
You copy text into the secondary selection using the Meta key plus the mouse - for example, press and hold Meta (the Alt key, usually) while dragging or double-clicking mouse-1.
You paste the secondary selection using Meta plus mouse-2.
Select text with the mouse, then copy it to the kill-ring using M-w. Then select the text to replace with the mouse and use C-y to paste the copied text to replace it.
Using the 'multiple-cursor' emacs package,
when I mark a word and cut ('C-w') in different multiple cursors, the only word in the 'active' cursor I control is cut.
I want to select different words in multiple positions and yank it to other relative position.
When I first tried this I remember some yes or no option at the screen bottom, but I couldn't really notice this at first and some keystroke made it to "no". I guess this might have been an option for multiple-cut. But I don't know how to revert this choice.
How do you do multiple cut and yank in emacs?
This works by default, so at some point you have indeed asked the library not do do this.
These settings are stored (by default) in ~/.emacs.d/.mc-lists.el
Just edit that file appropriately, and then M-x eval-buffer RET to make those same changes in the running instance.
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.
I'd like to be able to set multiple marks in Emacs like Vim does. In Vim you might press m b and that would set a mark at that line in the file. Later pressing ' b will move your cursor back to that line. You can make multiple marks with m{a-zA-Z}. Is there a way to have multiple marks like this in Emacs?
From Emacs documentation :
C-x r SPC r
Record the position of point and the current buffer in register r (point-to-register).
C-x r j r
Jump to the position and buffer saved in register r (jump-to-register).
But if you want your positions to persist automatically from one Emacs session to the next, you should use Bookmarks :
C-x r m RET
Set the bookmark for the visited file, at point.
C-x r m bookmark RET
Set the bookmark named bookmark at point (bookmark-set).
C-x r b bookmark RET
Jump to the bookmark named bookmark (bookmark-jump).
C-x r l
List all bookmarks (list-bookmarks).
M-x bookmark-save
Save all the current bookmark values in the default bookmark file.
You can use what Emacs calls registers. The documentation explains them better than I can.
Try the mark ring for quick marks:
C-spaceMake a mark at current position; also, add position to mark ring.
C-xC-xJump back to previous mark.
C-UC-spaceCycle through marks in the mark ring.
I used Vim for a decade before switching to Emacs a few years ago, and while the registers and bookmarks looked good at first, the mark ring is what I actually end up using 90% of the time. Usually I just use the C-space, C-x C-x, but cycling works, too.
Btw, realize that doing large non-arrow key movements like M-v will often add a mark to the mark ring. Just practice these key combos and you'll likely find them sufficient for most tasks.
Radix already did a good job explaining the registers and bookmarks, and those are useful for locations in files that will be referred to often or need annotation.
Vanilla Emacs makes you specify a name for each bookmark. What you want, it sounds like, is a quick way to create bookmarks without naming them -- just hit a key. You want autonamed bookmarks, available with Bookmark+. You can even have them be automatically highlighted, if you like (the fringe or the line).
Have a look at this: http://www.cs.utah.edu/dept/old/texinfo/emacs18/emacs_13.html
Unlike windows style self explanatory copy/cut/paste commands, I could not understand ring concept in emacs.
Since I don't program very often in emacs, I could have not realized the value of ring feature. Can you tell me what is called ring in emacs and how to use it?
Well, let me try in simple words. Each time you copy (M-w) or cut (C-w), the selection is inserted into a top of so called ring (which is just like a closed ended list).
The interesting part comes after. Now, if you paste with C-y then the most recently pasted region (which is now the front element of the ring) is inserted in the buffer. If you continue pressing M-y, then the text is replaced successively with older and older elements from the ring, so to say, the ring is rotated and the older text is designated as front element each time. This gives you an access to older cut/copied text.
The useful part does not end here. If you cut/copy some other text, it will be inserted at the top of the ring, and the ring will be rotated again such that the top is now the front. And you can start the C-y M-y sequence again, with the newly inserted text designated as front.
So, to conclude:
the top of the ring is the place where the newly copied/cut (M-w/C-w) text is inserted. When that happens, the top element becomes the front element.
the front element of the ring is the place on which paste (aka yank) commands C-y and M-y operate, by inserting it into the buffer. M-y also rotates the ring, such that older text in the ring becomes the front.
You can visualize it by imagining the circle with the front fixed at 12 hours position, but not part of the ring. The top is part of the ring instead, so when some command rotates the ring the top is also rotated.
Every time you copy or cut something to the clipboard in windows, you lose whatever was on your clipboard before. (Though some programs will store previous clipboard contents for you) The emacs "ring" will store old clipboard contents even after you copy/yank/cut/kill things. It can be handy if you get used to it because it lets you store more than one thing in the clipboard at once, and reduces the chances of accidentally overwriting something that you cut to the clipboard like you could in Windows.
A ring is a circular buffer. Think of a rolodex.
If you are killing/yanking to a ring, then you can walk forward and backwards in your history.
On Ubuntu, with the emacs-goodies-el package installed:
Press C-( M-x browse-kill-ring RET C-)
This defines a temporary keyboard macro which calls browse-kill-ring
when you press C-x e.
Now you don't have to just read about the kill ring, you can actually see what's in it.
Experiment with C-w (cut), M-w (copy), C-y (yank) and C-y M-y (yank next in ring), and press C-x e to see the effect on the kill ring.
From the Emacs manual: "A ring is a fixed-size data structure that supports insertion, deletion, rotation, and modulo-indexed reference and traversal." In other words, it's a circular queue.