I do this a lot: [M-%] to query-replace, then hit [y] a few times, then I'm past the area where I'm changing things so I hit [q] to be done.
Then I'd like to get the cursor back to where I was, so here's where it gets awkward. I [C-_] to undo the last replace, then move the cursor on character, then undo again to redo the last replace. Now the cursor's up where I've been editing, but there has to be a better way.
(I'm happy to get back to either where I started the replace, or where the last replace happened.)
C-uC-SPC jumps to the mark (which was automatically set as Blrfl pointed out), and then pops the previous mark off the local mark ring (so you can repeat the process to move to previous marks).
I think this is preferable to exchange-point-and-mark, as the latter reactivates the mark, highlighting the region in transient-mark-mode, which you wouldn't want in this case. (Unless you wanted to jump back and forth between the point you started at and the location of the final replacement, in which case exchange-point-and-mark would be exactly what you wanted.)
After you quit, the mark will be where you started and the point will be where you stopped. C-x C-x will swap the two, putting the point where you started.
Related
I find myself often recording a macro, marking a region, and then realising I didn't want to mark a region. I then hit C-g, my way to stop it, but then as a result it stops the macro.
While executing the macro, I wouldn't care if there is an unnecessary marking and demarking, as long as I can just quickly do the macro.
Is there a way to silently stop marking a region?
If you didn't mean to set the mark, just use a prefix argument to the set-mark-command binding to pop that mark (jumping to it, and deactivating the region):
C-uC-SPC
If you don't want to jump to that position, then you'll be stuck with having the extra mark(s) in the mark ring, but you can deactivate the region by using the command (with no prefix) twice:
C-SPCC-SPC
That sets and then deactivates the mark.
For clarity, note that buffers you are editing will tend to have a "marked region" most of the time -- there is always a point value, and there is generally a current mark value if you have been editing. I am assuming you are actually referring to the "active region" (i.e. a highlighted region, assuming that transient mark mode is enabled).
I understand that after yanking (C-y) I can move backwards through the kill ring (M-y). But sometimes I move past the desired yank - is there a way to move forward in the kill ring? Basically, I want the opposite of M-y.
If this is a duplicate, just let me know. I cannot seem to find my answer on SO or Google.
There is also M--M-y which is a shorter equivalent to C-u-1M-y.
You can prefix arguments with yank-pop. From the documentation of C-hfyank-popRET
With no argument, the previous kill is inserted.
With argument N, insert the Nth previous kill.
If N is negative, this is a more recent kill.
So for example to move 'forward' by one entry after yanking you can do C-u-1M-y. You can also simply use undo as #seanmcl suggests.
It can easily get difficult to keep track of entries in the kill-ring. So I would recommend an extension like browse-kill-ring which displays the kill ring and allows you to select the text to be yanked.
You can simply `undo' when you move past it. Repeated undos will continually move forward. I agree with #Iqbai though that browse-kill-ring is more appropriate for long chains of yanks.
Let's say you are about to type something that you know you want to kill-yank later. For example, say we need to repeat the following 10 times:
grid.addValue([some variable])
Currently, before I start typing, I can press C-<spc> C-<spc> to activate then deactivate the mark at the beginning of the text. Then, once I finish typing grid.addValue(), I can press C-x C-x twice to go back to the original mark, then back to the end of the text with the text highlighted.
My question: is there a way to set the mark and highlight as I type, so when I am done with the phrase I can immediately copy and yank it a bunch of times?
Just set the mark, type, and then copy/kill as normal.
The region (i.e. whatever is between point and mark) isn't highlighted, but it's still usable.
In Emacs I can edit something at location 'b' and do C-u C-Space to back to the last place I was, location 'a', in the buffer. But how do I get back to 'b'? In short instead of popping the mark ring stack, I want its history preserved.
I have read the documentation on using bookmarks, registers and tags but I have to manually set those. As in the case of returning from a single detour using C-u C-Space I don't want to have to remember to set anything.
I have many use-cases for this, but one that comes up most often is going back and forth between the "import ..." section which is usually at the top of my program and my current edit location.
C-x C-x swaps the position of the point (i.e. the cursor) and the mark. You can use this to go back and forth between two positions.
This question is indeed a duplicate of "How to move forward and backward in Emacs' mark ring". Thanks Sean!
In vim, visual block can be recall by 'gv' command so that multiple commands can be applied easily. (such as, comment out, then indent, then do_something_fun).
In Emacs, how can this be achieved?
[C-xC-x] only works when current cursor position stays where previous block ended.
If previous block was changed, the closest is to go through 'point-to-register' and 'jump-to-register'.
Just I am curious if there is an Emacs built-in command making this in one shot.
If Transient Mark mode is off, the region is always active. If it's on (which it sounds like is your situation), you can set mark-even-if-inactive to non-nil to allow region commands to work while the region isn't highlighted.
However, note you also can cycle back through previous mark positions using C-u C-SPC -- this will pop the mark ring. Once you're back to where you want to be, C-x C-x will rehighlight the region you want. (It may take a little bit of playing with this feature to get a feel for it, but it's why I can't switch away from Emacs now.)
If I understand correctly what you are asking for, then you don't need to do anything. When you select a region in emacs, it stays selected until you select a new one. So you could select the region and then perform as many actions as you want.
Sounds like you're looking for the secondary selection, which stays put even as the region might change. (It stays put until you move it.)
See:
the Emacs manual, node Secondary Selection
Emacs wiki page Secondary Selection
library second-sel.el:
Also narrow-to-region (CTRL-x n n ) applies every command from then on just to that region- you can't hurt the rest of the buffer, it doesn't even show. After done editing , widen (CTRL-x n w )to get back the whole buffer.
CMM
If you use evil-mode, just press gv like in vim.
Since the answers here and for other similar SO questions didn't help for me (CUA-mode, Emacs 24, not only indent-rigidly), I continued searching and finally found a reselect-last-region defined in this collection of custom function (starting line 670). That worked like a charm for me - and hopefully does for others still arriving here.