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!
Related
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 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.
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 am doing a search backwards in the text in emacs, then I move the point around and do a modification or copy, but then can I return to the point of the search before or do I still have to search for text around that spot?
Best,
Try
M-x pop-to-mark-command
And you can also get that functionality by C-u SPC or C-u C-# (i.e. using a prefix argument with the set-mark-command).
For more information on the mark ring, read about the mark ring. There is also a global mark ring, which is basically a list of marks across buffers, which you can navigate via C-x C-SPC.
I've tweaked some elisp, point-stack to allow for quick forward/backward through buffers. I've written more on my blog, but the basic idea is f5 to push a location, f6 to jump back, f7 for forward.
Here's and animated gif showing it.
Ctrl+R foo Enter Notice that “Mark saved where search started” message?
... edit ...
Ctrl+U Ctrl+Space Go back to the saved mark
If you used the mark during the “edit” part, press Ctrl+U Ctrl+Space again that many times. The successive marks are stored on a ring (for simple uses, what you see is a mark stack): each time the mark is set, it's pushed onto the stack, and Ctrl+U Ctrl+Space goes to the mark popped from the stack.
You can also use Ctrl+U 42 Ctrl+Space or Ctrl+4 Ctrl+2 Ctrl+Space to go back to the 42nd previous mark.
If you put (setq set-mark-command-repeat-pop t) in your .emacs, you can repeat just Ctrl+Space after Ctrl+U Ctrl+Space to go back to successive previous marks.
Before search use Ctrl-space to save your mark. Then do a seach and copy some text. Then, use Ctrl-u-Ctrl-space to pop back to your saved mark.
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.