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.
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.
How can I get scrollbars with exact positioning?
To illustrate what I mean:
C-h C-f (Read the The GNU Emacs FAQ)
Now try to position the buffer content such that the paragraph starting with This is the version ... is on top. With the default scrollbar you need several redraws to do this. Or you left-click for cursor positioning then C-lC-l
Now go back to where you have been before. This is impossible with regular scrollbars.
What I would like to have is to just click in the scrollbar at the height of This is the version ... to position this line on top for step 2. And to go back where I came from, I rightclick (without having moved the mouse, indeed).
In the past, I have always installed Emacs with the Athena widgets-like option to get such scrollbars, see a description of them. But I wonder if there is now a better or more modern way to do this. After all, I'd rather like to use standard distributions.
Maybe, it is not even necessary to change the scrollbars at all but to use rather the left-fringe for it. After all, mouse clicking in the fringe is recognized as <left-fringe>
M-x emacs-version
GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-21 on batsu, modified by Debian
Maybe a change of usage habits could help? Some Emacs user recommend disabling the scrollbars completely and navigate Emacs exclusively with the keyboard. (This habit is also beneficial for using Emacs inside a shell.)
Emacs provides a large number of navigation shortcuts. For instance, for jumping up and down large amounts of buffer space, you have:
C-v scroll-up
M-v scroll-down
M-} forward-paragraph
M-{ backward-paragaph
C-x ] forward-page
C-x [ backward-page
However, I would argue that by far the most useful navigation commands are:
C-s isearch-forward
C-r isearch-backward
In Emacs, search is so cheap, it is such an integral part of the work flow that it's often times much faster to get to a different point in the buffer by using a reference word as an anchor for a quick search, rather than leaving the home row to reach for the mouse. Hitting C-s or C-r repeatedly will move the point to consecutive matches. (You can even switch to regular expression search by hitting M-r while searching to make search even more powerful.)
You already mentioned C-l C-l to view the current line at the top of the buffer.
Now, if you want to go back to the previous location, you could use
C-u C-x
which jumps back to the mark. All the above commands for navigation push their start position on a "mark-ring" which means you can not only jump back one position, repeatedly hitting C-u C-x will take you back to many previous positions.
For this task I press C-l once, twice, three times.They will switch between middle-top-bottom.
I want as I search backwards in the document to do copying (M-x) and then return to the saved mark after a few copying commands and paste the things I copied. What ways are there to do this?
Best,
Do you mean you want to paste all of the things you copied in a single go?
One way is to do the first copy via M-w (aka M-x kill-ring-save), and the subsequent copies using C_M-w M-w. You can read about that in the docs here.
If you want to paste the chunks you copied individually, you can cycle through the pastes by doing C-y M-y M-y ..., where each successive M-y replaces the previous yank with the prior entry in the kill ring.
One nice mode for choosing which element from the kill ring to paste is the BrowseKillRing package.
Not too sure what you're asking, but it sounds like you want random (i.e., direct) access to any entry in the kill ring.
In Icicles you can see all of your kill-ring, filter it dynamically to see only some of it, and yank any entries from it using completion. By default, C-y is bound in Icicle mode to icicle-yank-maybe-completing.
That's the same as yank, unless you give it a negative prefix arg (e.g., C--). In that case, it lets you complete against the kill-ring. Completion can be prefix, apropos (substring, regexp), or fuzzy.
http://www.emacswiki.org/emacs/Icicles_-_Multi-Commands
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
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.